0

我有兩個ApplicationUsers,Cursist和講師web應用:外鍵衝突添加ApplicationUser到數據庫結果

public class ApplicationUser : IdentityUser 
{ 
    public string Sex { get; set; } 
    public string FName { get; set; } 
    public string LName { get; set; } 
    //email inherited by IdentityUser 
    .... 
} 

public class Cursist: ApplicationUser 
{ 
    public Group Group { get; set; } 
    public int GroupId { get; set; } 
    .... 
} 

public class Lector: ApplicationUser 
{ 
    private IEnumerable<Group> _supervising; 
    .... 
} 

這是我第一次使用與ApplicationUser,這就造成了一個恆定的錯誤繼承:

System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AspNetUserClaims_AspNetUsers_UserId". The conflict occurred in database "cvodb", table "dbo.AspNetUsers", column 'Id'. The statement has been terminated.

錯誤的起源似乎來​​自這個代碼片段:

public async Task InitializeData() 
    { 
     _dbContext.Database.EnsureDeleted(); 
     if (_dbContext.Database.EnsureCreated()) 
     { 
      await InitializeUsers(); 
      _dbContext.SaveChanges(); 
     } 
    } 

    private async Task InitializeUsers() 
    { 
     ApplicationUser user = new Cursist("Male", "fname", "lname", "[email protected]"); 
     await _userManager.CreateAsync(user, "password"); 
     await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "Cursist")); 
    } 

更改後編輯 我設法通過向UserUserId添加UserName來解決UserId衝突。該班現在這個樣子:

public class ApplicationUser : IdentityUser 
{ 
    public string Sex { get; set; } 
    public string FName { get; set; } 
    public string LName { get; set; } 
    //email inherited by IdentityUser 

    public ApplicationUser(string Sex, string fname, string lname, string email) 
    { 
     base.Email = email; 
     //username fixes foreign key conflict? 
     base.UserName = email; 
     Sex = sex; 
     FName = fname; 
     LName = lname; 
    } 
} 
public class Cursist: ApplicationUser 
{ 
    public Group Group { get; set; } 
    // can be null 
    public int? GroupId { get; set; } 
} 
//Lector class hasn't changed. 

我得到這個輸出在控制檯(忽略拼寫差異):

ALTER TABLE [AspNetUsers] ADD CONSTRAINT [FK_AspNetUsers_Groep_GroepId] FOREIGN KEY ([GroepId]) REFERENCES [Groep] ([GroepId]) ON DELETE NO ACTION; Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

ALTER TABLE [AspNetUsers] ADD CONSTRAINT [FK_AspNetUsers_Groep_GroepId1] FOREIGN KEY ([GroepId1]) REFERENCES [Groep] ([GroepId]) ON DELETE NO ACTION; Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

ALTER TABLE [AspNetUsers] ADD CONSTRAINT [FK_AspNetUsers_Groep_GroepId2] FOREIGN KEY ([GroepId2]) REFERENCES [Groep] ([GroepId]) ON DELETE NO ACTION;

我不知道爲什麼AspNetUsers正在創建的組id 3個實例?一審和二審產生任何錯誤,但最後一個作用:

{Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AspNetUsers_Groep_GroepId2". The conflict occurred in database "cvodb", table "dbo.Groep", column 'GroepId'. The statement has been terminated.

我有一種感覺它試圖鏈接的組對象,但一組可以是空值。我仍然不確定爲什麼有一個groupId,groupId1和groupId2。我有一個叫Group的類和兩個子類ClosedGroup和OpenGroup。他們應該使用相同的密鑰GroupId,沒有Group的實例。只有ClosedGroups和OpenGroups。

+1

我假設用戶被創建並返回給你,但是異常在'_userManager.AddClaimAsync'的最後一行。在運行_userManager.CreateAsync後運行調試器並檢查'user'對象。 ID屬性是否填充? – trailmax

+0

@trailmax用戶確實已創建並返回。 Id字段具有以下值:「e8d2a128-08de-4520-95f4-75ede1a51c1c」,所以我想它已成功填充。 – Tumladhir

+0

顯然,當我將用戶名添加到ApplicationUser時,衝突得到解決。然而,新出現的陌生人衝突出現了。更新問題。 – Tumladhir

回答

1

我敢打賭,這個問題是這個問題最受打擊。嘗試添加claim時,衝突是由一個或多個模型錯誤引起的。大多數情況下,user已成功創建,但添加索賠的過程因模型錯誤而中斷。首先,而非火&忘不了CreateAsync,將其分配給一個變量,如下所示:

var user = await _userManager 
         .CreateAsync(superadmin, "Password"); 

將鼠標懸停在user variable確定遇到的模型誤差,如下所示的圖像:

enter image description here

在我的情況下,我沒有分配所需的Email字段。然後,您可以修復模型錯誤,衝突將消失。乾杯!