When I try Membership.DeleteUser(string, bool)
in a MVC 4 project where I use SimpleMembership
provider, I get following error message:
"The DELETE statement conflicted with the REFERENCE constraint "fk_UserId". The conflict occurred in database "Conductor_Basic3", table "dbo.webpages_UsersInRoles", column 'UserId'. The statement has been terminated."
The constraints is set and is correct, so I can't really understand the error message. I guess I've missed something here.
Can anyone explain to me this error?
Looks like the table webpages_Roles has reference to your user, you should to exclude user from role, and after that you able to delete user, try this:
Roles.RemoveUserFromRole("UserName","RoleName");
Membership.DeleteUser("UserName");
Or you can try to use another approach: explicitly specify cascade deletion in your db like this:
It looks like the foreign key constraint doesn't let you delete rows from the user table because they have matching rows in dbo.webpages_UsersInRoles tables. I am not very familiar with default Membership Provider schema but you have to make sure you delete the rows in dbo.webpages_UsersInRoles for getting past that error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With