Error when deleting user mvc 4 using SimpleMembership

偶尔善良 提交于 2020-01-02 05:36:29

问题


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?


回答1:


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:




回答2:


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.




回答3:


you can try this:

[Authorize(Roles = "Admin")]
[HttpPost]
public ActionResult DeleteUser(int id)
{
    var tmpuser = "";
    var ctx = new UsersContext();
    using (ctx)
    {
        var firstOrDefault   = ctx.UserProfiles.FirstOrDefault(us => us.UserId==id);
        if (firstOrDefault != null)
            tmpuser = firstOrDefault.UserName;
    }

    string[] allRoles = Roles.GetRolesForUser(tmpuser);
    Roles.RemoveUserFromRoles(tmpuser,allRoles);

    //Roles.RemoveUserFromRole(tmpuser, "RoleName");

    ((SimpleMembershipProvider)Membership.Provider).DeleteAccount(tmpuser);
    Membership.Provider.DeleteUser(tmpuser, true);
    Membership.DeleteUser(tmpuser, true);

    ctx = new UsersContext();

    return View(ctx.UserProfiles.OrderBy(user => user.UserName).ToList());
}



回答4:


Had the same issue and solved it by expanding @testCoder 's answer a bit.

var rolesProvider = (SimpleRoleProvider)Roles.Provider;
var roles = rolesProvider.GetRolesForUser(username);
if (roles != null && roles.Length > 0)
{
    rolesProvider.RemoveUsersFromRoles(new[] { username }, roles);
}
Membership.DeleteUser(username, true);



回答5:


In addition to what was given by testCoder, here is some implementation which worked fine for me...

Public Shared Sub DeleteUser(User As String)
    Dim UserRoles = Roles.GetRolesForUser(User)
    If UserRoles.Count > 0 Then Roles.RemoveUserFromRoles(User, UserRoles)
    DirectCast(Membership.Provider, WebMatrix.WebData.SimpleMembershipProvider).DeleteAccount(User)
    DirectCast(Membership.Provider, WebMatrix.WebData.SimpleMembershipProvider).DeleteUser(User, True)
End Sub


来源:https://stackoverflow.com/questions/12992228/error-when-deleting-user-mvc-4-using-simplemembership

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!