I have the concept of user and roles where User
has many roles and Role
has many users. If a user is in a role (using a link table generated by EF), and I try to delete it, it does not cascade, then it throws an error of : The primary key value cannot be deleted beca开发者_StackOverflowuse references to this key still exist. [ Foreign key constraint name = Role_Users_Target ]
.
This is the code I use to delete the user:
var user = new User() { UserId = userId };
db.Users.Attach(user);
db.Users.Remove(user);
db.SaveChanges();
How do I fix this?
精彩评论