[英]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.