Microsoft Developer Network >
Forums Home
>
Data Platform Development (Pre-release) Forums
>
ADO.NET Entity Framework and LINQ to Entities (Pre-Release)
>
changes to complex type not seen by state manager.
changes to complex type not seen by state manager.
- 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();ThanksZeeshan Hirani
Answers
- 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.- Marked As Answer byDiego B VegaMSFT, ModeratorThursday, November 05, 2009 8:14 AM
- Proposed As Answer byDiego B VegaMSFT, ModeratorThursday, November 05, 2009 6:14 AM
All Replies
- 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.- Marked As Answer byDiego B VegaMSFT, ModeratorThursday, November 05, 2009 8:14 AM
- Proposed As Answer byDiego B VegaMSFT, ModeratorThursday, November 05, 2009 6:14 AM

