AspNetUserRoles 表中有兩個額外的外鍵(TheUsrId、TheRoleId),如下圖所示。

這是配置:
public class AppUser : IdentityUser<int>
{
... // other properties
public ICollection<AppUserRole> TheUserRolesList { get; set; }
}
public class AppRole : IdentityRole<int>
{
public ICollection<AppUserRole> TheUserRolesList { get; set; }
}
public class AppUserRole : IdentityUserRole<int>
{
public AppUser TheUser { get; set; }
public AppRole TheRole { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AppUser>(b =>
{
b.HasMany(x => x.TheUserRolesList)
.WithOne(x => x.TheUser)
.HasForeignKey(x => x.UserId)
.IsRequired();
});
modelBuilder.Entity<AppRole>(b =>
{
b.HasMany(e => e.UserRoles)
.WithOne(e => e.Role)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
});
base.OnModelCreating(modelBuilder);
}
我已經嘗試了多種方法,例如將名稱更改為TheUsertoUser但最終,我將UserId1其作為外鍵。
uj5u.com熱心網友回復:
當您不在這些類中撰寫任何內容時,ASP.NET Core Identity 會自行在 AppRole 和 AppUserRole 中創建自己的關系和列。
在 AppUserRole 中,當您創建 TheUser 和 TheRole 屬性時,物體框架會在其末尾添加“Id”并創建新的關系。所以這就是物體框架的規則。
您應該將它們全部洗掉并嘗試查看原因。
uj5u.com熱心網友回復:
最后,我注意到這是 EntityFrameworkCore 版本 5.0.4 中的一個錯誤,并且在更新到 5.0.17 后問題得到了解決。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/473958.html
