2013-03-03 165 views
0

當執行: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

的錯誤似乎相當自我解釋。 '[FK_dbo.webpages_UsersInRoles_dbo.webpages_Membership_UserId]'是不是一個約束,在表中的兩個限制是'fk_UserUd'和'fk_RoleId'。你想要放棄什麼約束? – GarethD 2013-03-03 17:49:02

+0

林試圖重新與此 的UsersInRoles [表( 「webpages_UsersInRoles」)] 公共類UsersInRole { [密鑰,列(訂單= 0)] 公衆詮釋角色ID {得到;組; } [密鑰,列(訂單= 1)] 公衆詮釋用戶ID {得到;組; } [柱( 「角色ID」),InverseProperty( 「UsersInRoles」)] 公衆角色角色{得到;組; } [柱( 「用戶ID」),InverseProperty( 「UsersInRoles」)] 公衆成員成員{得到;組; }} 我應該已經運行,但它不會因爲約束。 – Bormeth 2013-03-04 07:42:27

回答

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