SimpleMembership无法删除约束

时间:2013-03-03 17:18:28

标签: sql model-view-controller simplemembership

执行时:

ALTER TABLE [dbo].[webpages_UsersInRoles] DROP CONSTRAINT [FK_dbo.webpages_UsersInRoles_dbo.webpages_Membership_UserId]

我收到错误消息:

'FK_dbo.webpages_UsersInRoles_dbo.webpages_Membership_UserId' is not a constraint.
Could not drop constraint. See previous errors.

这是我的webpages_UsersInRoles表:

CREATE TABLE [dbo].[webpages_UsersInRoles] (
    [UserId] INT NOT NULL,
    [RoleId] INT NOT NULL,
    PRIMARY KEY CLUSTERED ([UserId] ASC, [RoleId] ASC),
    CONSTRAINT [fk_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[UserProfile] ([UserId]),
    CONSTRAINT [fk_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[webpages_Roles] ([RoleId])
);

我尝试按照此answer进行操作,但它对我不起作用。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

[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());
}