1

As an example I have a database with two tables , company and employee with a one to many relationship. I am using EF 4

I was going through Julie Lerman's book and the firestarter sessions on channel 9 and have some questions on using dynamic proxies

  1. It is mentioned that when using dynamic proxies, creation of new objects should be done through ObjectContext.CreateObject and not by saying new Object() (for reference 1:09 in the video mentioned above). She clearly says that if you try adding an object in this manner it wont get added to the database and I won't even get an error. However I tried the following and it worked

    try
        {
            using (var context = new sandeepTestEntities1())
            {
                var company1 = (from c1 in context.Companies
                                where c1.Name == "xyz"
                                select c1).Single();
    
                Employee e = new Employee();
                e.CompanyId = company1.Id;
                e.Age = 25;
                e.Name = "James";
    
                context.Employees.AddObject(e);
    
                context.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    

    I am unable to understand the use of the CreateObject method in this scenario. I felt that it might have something to do with relationship and foreign key fixup but even that works when using the code above.

  2. It is also mentioned that if we have any custom code in the setters for the entities and we are using dynamic proxies the code doesnt get called. (1:14 in the video). In my example for the Employee entity I have the following code for the name property

    public virtual string Name
    {
           get{return _name;} 
            set
                {
                    if (value != null && value.Length > 50) 
                     {throw new ArgumentException("Name must be less than 50 characters");}
                else
                { _name = value;}
                }
    
        }
    

I chnaged the code above for creating an employee to

   try
        {
            using (var context = new sandeepTestEntities1())
            {
                var company1 = (from c1 in context.Companies
                                where c1.Name == "xyz"
                                select c1).Single();

                var e = context.CreateObject<Employee>();
                e.CompanyId = company1.Id;
                e.Age = 25;
                e.Name = "X".PadLeft(51,'.');

                context.Employees.AddObject(e);

                context.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

and I still get an exception when assigning the Name indicating that my custom code is being called. Is my understanding of both these points incorrect ?

nighthawk457
  • 1,102
  • 3
  • 12
  • 27
  • The statements are probably wrong (you have proven it). Here is an example when using `Create` is important: http://stackoverflow.com/a/7312233/270591 It's about `DbContext` but the same applies to `ObjectContext` and `CreateObject`. – Slauma Jan 20 '12 at 16:53

0 Answers0