Search code examples
entity-frameworkentity-framework-4entity-framework-ctp5ef-code-first

can't delete object that has a many-to-many relationship


these are my simplified entities:

public class User : Entity
{
    public virtual ICollection<Role> Roles { get; set; }
}

public class Role : Entity
{
    public virtual ICollection<User> Users { get; set; }
}

var user = dbContext.Set<User>().Find(id);
dbContext.Set<User>().Remove(user);
dbContext.SaveChanges(); // here i get error (can't delete because it's the     
//referenced by  join table roleUsers

the problems is that the join table references the user table and ef doesn't remove the records from the join table before deleting the user

I tried writing test cases and I noticed that:

if use the same context to add user with roles, save changes, remove and again save changes it works

but if I use 2 different contexts one for insert and another one for delete I get this error


Solution

  • You must first clear Roles collection (user's roles must be loaded) before you will be able to remove user.