Hi Arthur.
Thank you for your very clear explanation. You’re right, when you see it as actually destroying the relation opposed to deleting the entity, it makes perfect sense.
I will vote for that feature. Thanks again.
Van: [email removed]
Verzonden:maandag 16 december 2013 19:18
Aan: [email removed]
Verzonden:maandag 16 december 2013 19:18
Aan: [email removed]
From: ajcvickers
@Sebazzz The behavior of the example you show is as expected with regards to the way EF currently handles relationships. I will try to explain. First, when you remove an entity from a collection navigation property this
is treated as a severing of the relationship between the two entities. Just because the relationship has been severed does not necessarily mean that the entity on the dependent end of the relationship will be deleted. For example, the removed Child could be
re-parented to another different Project, which would mean that the FK in the database is updated rather than the row being deleted.
All that being said, if the FK is nullable, then this state can be saved to the database (without re-parenting) by saving null as the FK. If the FK is not nullable then a good argument can be made for automatically deleting these orphaned entities as part of SaveChanges. If this is something you would benefit from then you can vote for the feature here: http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1263145-delete-orphans-support. Additional information on deleting orphans is here: http://blog.oneunicorn.com/2012/06/02/deleting-orphans-with-entity-framework/
Thanks,
Arthur
All that being said, if the FK is nullable, then this state can be saved to the database (without re-parenting) by saving null as the FK. If the FK is not nullable then a good argument can be made for automatically deleting these orphaned entities as part of SaveChanges. If this is something you would benefit from then you can vote for the feature here: http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1263145-delete-orphans-support. Additional information on deleting orphans is here: http://blog.oneunicorn.com/2012/06/02/deleting-orphans-with-entity-framework/
Thanks,
Arthur