Ask a questionAsk a question
 

Answerchanges to complex type not seen by state manager.

  • Wednesday, November 04, 2009 11:09 PMzeeshan hirani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a customer class with Name as being complex type. In my poco implementation when i modify the complex type the state of the entity does not change to modified. Also when i call db.CreateObject<Customer>(), it does not initialize my complex type to an empty instance. I thought if you use generated code your complex type will always be initialized to an empty instance and u just have to set the value. Hmm why does CreateObject not do that for POCO object? Also why does complex type is not getting a proxy? I only see proxy on customer entity?


    public class Customer
    {
        public virtual int CustomerId { get; set; }
        public virtual Name Name { get; set; }
    }
    public class Name
    {
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
    }

    var db = new POCOContext();
    var customer = db.CreateObject<Customer>();

    customer.Name = new Name { FirstName = "John", LastName = "Parket" };
    db.Customers.AddObject(customer);
    db.SaveChanges();
    var entry = db.ObjectStateManager.GetObjectStateEntry(customer);

    Console.WriteLine("Before State:{0}",entry.State);
    customer.Name.LastName = "Matt";
    Console.WriteLine("After State:{0}", entry.State);
    db.SaveChanges();


    Thanks
    Zeeshan Hirani

Answers

  • Thursday, November 05, 2009 6:05 AMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Zeeshan,

    This is a good observation. We don't create proxies for complex types. Instead we detect when you replace the whole instance and otherwise perform snapshot comparisons to detect changes in nested properties. I you need to able to read in what the state of a complex type properties is before you call SaveChanges, you can call DetectChanges.

    We considered various alternatives, and proxies didn't provide enough benefits, but this is an area that we will probably revisit in the future.

    You are also right that we don't create complex instance when you call CreateObject<T> to obtain an entity. I suggest you create the complex type instance lazyly in the property getter in your POCO. Incidentally, complex type proxies would not work very well with this pattern: you don't have access to the ObjectContext from inside the entity, and so you have to use "new" instead of CreateObject<T>.

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Thursday, November 05, 2009 6:05 AMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Zeeshan,

    This is a good observation. We don't create proxies for complex types. Instead we detect when you replace the whole instance and otherwise perform snapshot comparisons to detect changes in nested properties. I you need to able to read in what the state of a complex type properties is before you call SaveChanges, you can call DetectChanges.

    We considered various alternatives, and proxies didn't provide enough benefits, but this is an area that we will probably revisit in the future.

    You are also right that we don't create complex instance when you call CreateObject<T> to obtain an entity. I suggest you create the complex type instance lazyly in the property getter in your POCO. Incidentally, complex type proxies would not work very well with this pattern: you don't have access to the ObjectContext from inside the entity, and so you have to use "new" instead of CreateObject<T>.

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.