开发者

Entity framework delete cascade on many to many detached

开发者 https://www.devze.com 2023-02-24 07:25 出处:网络
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 i

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消