Delete Entity with Complex Property
- Hi,
I have an order with a complex property representing the shipping address. When I delete the order, I just create an instance, set the Id and then call attach, DeleteObject and SaveChanges. On SaveChanges I get an Exception stating that the ShippingAddress cannot be null. Well, if I want to remove the order I don't need to create the complex type instance.
I understand that I can solve this problem simply instantiating the address in the constructor, I just want to know why it is required.
Byez
Stefano
Answers
Stefano,
Currently we only track whether a property of the entity is modified, so a complex type property in an existing entity is always going to be either completely modified or unchanged.
This means that current values for all nested properties are sent in UPDATE statements, but also if any property in the complex type is used for concurrency control, then the original values for all nested properties are required and sent as part of the WHERE clause in the UPDATE statement.I consider this an implementation detail and something we could choose to change in the future, in the sense that we could have finer granularity in how we track changes.
Hope this helps,
Diego
This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed As Answer byDiego B VegaMSFT, ModeratorMonday, October 26, 2009 10:32 PM
- Marked As Answer bysm15455MVPMonday, October 26, 2009 11:06 PM
All Replies
Hi Stefano,
For Entity Framework complex types are structural part of the entities, and a null value is not supported. We don’t validate this requirement is met across all components and methods, but we do in our update pipeline, and that is the reason you are not seeing an exception until you call SaveChanges.
One simple pattern you can follow when writing the code of your entities (I am assuming that you are using POCOs, but let me know if I am wrong) is to lazily create the complex type instance in the property getter.
Although complex types are always required today, and we assume their absence is a coding error, I suppose we could remove this requirement in the future.
Thanks for the feedback!
Diego
This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed As Answer byDiego B VegaMSFT, ModeratorSaturday, October 24, 2009 6:31 AM
- Hi Diego,
thanks for your reply. There is another thing about complex type I Would like to know. When I update an entity and modify a scalar property of a complex property, all the other properties of the complex type are updated even if they are not modified.
Could you explain me why this happens?
Byez
Stefano Stefano,
Currently we only track whether a property of the entity is modified, so a complex type property in an existing entity is always going to be either completely modified or unchanged.
This means that current values for all nested properties are sent in UPDATE statements, but also if any property in the complex type is used for concurrency control, then the original values for all nested properties are required and sent as part of the WHERE clause in the UPDATE statement.I consider this an implementation detail and something we could choose to change in the future, in the sense that we could have finer granularity in how we track changes.
Hope this helps,
Diego
This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed As Answer byDiego B VegaMSFT, ModeratorMonday, October 26, 2009 10:32 PM
- Marked As Answer bysm15455MVPMonday, October 26, 2009 11:06 PM

