I noticed something interesting when I was performing a delete using EF code first. I use the following domain model:
public class User
{
public virtual
This is how EF works. The problem is that your Playlist
forms entity graph with other relations and EF uses very simple rule for tracking entity graphs: All entities in the graph must be tracked - there cannot be reference to entity which is not tracked. I don't give you reference to description of this rule, it is just my observation but I didn't find any single exception to this rule.
Edit: Updated version - I just checked internal implementation and relations are indeed nulled during calling Delete
So what happened in your code.
Playlist
as deletedPlaylist
all related objects remain undeletedPlaylist
is non existing (deleted in the database) it was detached from the contextThe code responsible for nulling from System.Data.Objects.EntityEntry.Delete(doFixup)
(doFixup
is true) - the class is internal:
if (doFixup && (base.State != EntityState.Deleted))
{
this.RelationshipManager.NullAllFKsInDependentsForWhichThisIsThePrincipal();
this.NullAllForeignKeys();
this.FixupRelationships();
}
In your scenario this should have simple workaround - create DTO before you delete entity.