Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when deleting user mvc 4 using SimpleMembership

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?

like image 211
user1393252 Avatar asked Dec 11 '22 21:12

user1393252


2 Answers

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:

enter image description here

like image 129
testCoder Avatar answered Dec 14 '22 11:12

testCoder


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.

like image 28
Ufuk Hacıoğulları Avatar answered Dec 14 '22 09:12

Ufuk Hacıoğulları