[英]Asp.net core Identity "The INSERT statement conflicted with the FOREIGN KEY constraint "
我使用 ASP.NET CORE 身份創建 ASP.NET CORE 應用程序。 我創建種子 class 用於保存新用戶和角色以供首次啟動應用程序使用。 在這個種子 class 中,當我將角色添加到用戶時出現以下錯誤。
INSERT 語句與 FOREIGN KEY 約束“FK_AspNetUserRoles_AspNetUsers_UserId”沖突。 沖突發生在數據庫“DB_A14695_Elvinm”、表“dbo.AspNetUsers”、“Id”列中。 該語句已終止。
我使用以下 class 作為身份
public class ApplicationUser : IdentityUser
{
public int Type { get; set; }
public int Flags { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime LastModifiedDate { get; set; }
}
還有我的種子 class
public class DbSeeder
{
#region Private Members
private RvMusicalDbContext DbContext;
private RoleManager<IdentityRole> RoleManager;
private UserManager<ApplicationUser> UserManager;
#endregion Private Members
#region Constructor
public DbSeeder(RvMusicalDbContext dbContext, RoleManager<IdentityRole> roleManager, UserManager<ApplicationUser> userManager)
{
DbContext = dbContext;
RoleManager = roleManager;
UserManager = userManager;
}
#endregion Constructor
#region Public Methods
public async Task SeedAsync()
{
// Create the Db if it doesn’t exist
DbContext.Database.EnsureCreated();
// Create default Users
if (await DbContext.Users.CountAsync() == 0) await CreateUsersAsync();
}
#endregion Public Methods
#region Seed Methods
private async Task CreateUsersAsync()
{
// local variables
DateTime createdDate = new DateTime(2016, 03, 01, 12, 30, 00);
DateTime lastModifiedDate = DateTime.Now;
string role_Administrators = "Administrators";
string role_Registered = "Registered";
//Create Roles (if they doesn't exist yet)
if (!await RoleManager.RoleExistsAsync(role_Administrators)) await RoleManager.CreateAsync(new IdentityRole(role_Administrators));
if (!await RoleManager.RoleExistsAsync(role_Registered)) await RoleManager.CreateAsync(new IdentityRole(role_Registered));
// Create the "Admin" ApplicationUser account (if it doesn't exist already)
var user_Admin = new ApplicationUser()
{
UserName = "Admin",
Email = "admin@opengamelist.com",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate,
Flags = 0,
Type = 0
};
// Insert "Admin" into the Database and also assign the "Administrator" role to him.
if (await UserManager.FindByIdAsync(user_Admin.Id) == null)
{
await UserManager.CreateAsync(user_Admin, "Pass4Admin");
/// ERROR OCCURED HERE
await UserManager.AddToRoleAsync(user_Admin, role_Administrators);
// Remove Lockout and E-Mail confirmation.
user_Admin.EmailConfirmed = true;
user_Admin.LockoutEnabled = false;
}
#endregion Seed Methods
}
我想說角色成功保存了數據庫。
請幫助解決問題。
我知道這已經得到了回答,但我只需要將我的解決方案添加到我的問題中,希望我可以拯救其他一些可憐的靈魂免於重復我的錯誤......
我的用戶已創建...
我只是沒有單擊 SQL Server 對象資源管理器上的“刷新”按鈕。
:(真尷尬。我花了一個多小時來追蹤這個。
編輯:我看到我投了反對票。 我遇到了與提出問題的個人相同的問題,我的解決方案是頁面需要刷新。 盡管這是一個愚蠢的錯誤,但通常可以安全地假設,當您犯錯時,您不是唯一的錯誤。 在用戶沒有刷新頁面的所有情況下,我的答案都是正確的答案,我相信這是一個不錯的答案。
我有同樣的問題。 這就是解決方案; 轉到 DBContext 並刪除繼承類中的用戶。
前:
public class MyDBContext : IdentityDbContext<MyUser>
{ }
后:
public class MyDBContext : IdentityDbContext
{ }
我遇到了同樣的問題,這里沒有一個答案有效,最終我找到了解決方案。 當您有自定義用戶表時,請驗證在您最近創建的指向表 AspNetUsers 的遷移引用中,而不是指向您的自定義 ApplicationUser 表。 如果沒有,請刪除數據庫,手動修改遷移文件並應用更改。
您是否應該確保用戶管理員具有 ID
if (await UserManager.FindByIdAsync(user_Admin.Id) == null)
應該
if (await UserManager.FindByIdAsync(user_Admin.Id) != null)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.