This has bugged me for some time, and caused me to tear my hair out in frustration during debugging a few times.
I have a collection e.g. PackageEdits on my Package. PackageEdit.PackageKey is a required foreign key on PackageEdit corresponding to the owning object, and I even have cascading delete so it seems quite clear to me that PackageEdits are not intended to exist in isolation without Packages.
So far so good. However, when I remove(edit#1) from the collection and call SaveChanges() the really bad part of debugging experience begins. I get a message saying that I have something missing its required foreign key.
Now while learning EF this caused me no end of frustration because often I've written a page of code and I don't know which particular entity operation action is the one causing the mysterious messages.
Now after having seen this a number of times I think I know what is going on: EF is being picky about removing things from collections not being the same as deleting them. And it wants me to explicitly delete them as well. However this still continues to frustrate me.
So discussion is:
Why doesn't EF infer the object is being deleted when we remove the object from the collection, and don't reattach it to any other package object, and thereby save new EF users all these painful experiences of figuring out what they need to do explicit delete of?
I have a collection e.g. PackageEdits on my Package. PackageEdit.PackageKey is a required foreign key on PackageEdit corresponding to the owning object, and I even have cascading delete so it seems quite clear to me that PackageEdits are not intended to exist in isolation without Packages.
So far so good. However, when I remove(edit#1) from the collection and call SaveChanges() the really bad part of debugging experience begins. I get a message saying that I have something missing its required foreign key.
Now while learning EF this caused me no end of frustration because often I've written a page of code and I don't know which particular entity operation action is the one causing the mysterious messages.
Now after having seen this a number of times I think I know what is going on: EF is being picky about removing things from collections not being the same as deleting them. And it wants me to explicitly delete them as well. However this still continues to frustrate me.
So discussion is:
Why doesn't EF infer the object is being deleted when we remove the object from the collection, and don't reattach it to any other package object, and thereby save new EF users all these painful experiences of figuring out what they need to do explicit delete of?