c# - Asp.net 核心标识 "The INSERT statement conflicted with the FOREIGN KEY constraint "

标签 c# asp.net-mvc asp.net-identity entity-framework-core seeding

我使用 ASP.NET CORE Identity 创建 ASP.NET CORE 应用程序。 我创建种子类来为首次启动应用程序保存新用户和角色。在这个种子类中,当我向用户添加角色时出现以下错误。

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AspNetUserRoles_AspNetUsers_UserId". The conflict occurred in database "DB_A14695_Elvinm", table "dbo.AspNetUsers", column 'Id'. The statement has been terminated.

我使用了以下类作为标识

public class ApplicationUser : IdentityUser
{
    public int Type { get; set; }
    public int Flags { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime LastModifiedDate { get; set; }
}

还有我的种子类

 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


}

我想说角色保存数据库成功。

请帮忙解决问题。

最佳答案

在错误发生之前检查 UserManager.CreateAsync 调用的输出。我猜您的用户没有得到保留,所以下一次调用因 FK 问题而失败。

如果您使用默认的身份密码要求(就像我尝试时那样),您会从用户创建调用中得到密码验证错误结果。

enter image description here

关于c# - Asp.net 核心标识 "The INSERT statement conflicted with the FOREIGN KEY constraint ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280345/

相关文章:

c# - 在 C# 中使用 linq 更新 xml

c# - C# 中的 Guice 等价物

asp.net-mvc - 复用 MVC 架构;有两层 UI : ASP. NET MVC 和 .NET Winforms

asp.net-mvc - 只有当用户的用户名与数据库中的电子邮件完全相同时,我才能登录

c# - Multi-Tenancy 应用程序中的IdentityRole

c# - Android 日期到 C# long DateTime-Ticks

c# - ServiceStack.Redis 服务可用性

css - MVC PagedList 的自定义 CSS

javascript - ASP.NET 用户在线状态存储

asp.net - 将 AspNetIdentity 与 IdentityServer3 一起使用时,User.Identity.Name 始终为 null