C# 找不到用户ID,请在迁移种子中添加用户和角色

C# 找不到用户ID,请在迁移种子中添加用户和角色,c#,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,我在Migrations/Configuration.cs中有这个种子 protected override void Seed(xxx.xxx.ApplicationDbContext context) { var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); var RoleManager

我在Migrations/Configuration.cs中有这个种子

    protected override void Seed(xxx.xxx.ApplicationDbContext context)
    {

        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
        string role = "Admin";
        string password = "Password@1234";

        //Create Role Admin if it does not exist
        if (!RoleManager.RoleExists(role))
        {
            var roleresult = RoleManager.Create(new IdentityRole(role));
        }

        //Create Admin users with password=123456
        var admin1 = new ApplicationUser();
        admin1.UserName = "admin1@admin1.com";
        admin1.Email = "admin1@admin1.com";
        admin1.EmailConfirmed = true;
        UserManager.Create(admin1, password);
        UserManager.AddToRole(admin1.Id, role);

        //Create Admin users with password=123456
        var admin2 = new ApplicationUser();
        admin2.UserName = "admin2@admin2.com";
        admin2.Email = "admin2@admin2.com";
        admin2.EmailConfirmed = true;
        UserManager.Create(admin2, password);
        UserManager.AddToRole(admin2.Id, role);

        //Create Admin users with password=123456
        var admin3 = new ApplicationUser();
        admin3.UserName = "admin3@admin3.com";
        admin3.Email = "admin3@admin3.com";
        admin3.EmailConfirmed = true;
        UserManager.Create(admin3, password);
        UserManager.AddToRole(admin3.Id, role);

        context.SaveChanges();           
    }
一些跟踪日志

[无效操作异常:未找到用户ID。]
Microsoft.AspNet.Identity.d_u83.MoveNext()+1279
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)+93
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+52 Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func
1
func)+266
Microsoft.AspNet.Identity.UserManager.AddToRole(UserManager
2 经理,TKey用户ID,字符串角色)+119
xxx.Migrations.Configuration.Seed(ApplicationDbContext上下文) +292 System.Data.Entity.Migrations.DbMigrationsConfiguration
1.OnSeed(DbContext
上下文)+42
System.Data.Entity.Migrations.DbMigrator.SeedDatabase()+102
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
1 pendingMigrations,字符串targetMigrationId,字符串lastMigrationId) +448 System.Data.Entity.Migrations.Dbmigator.UpdateInternal(字符串targetMigration)+446
System.Data.Entity.Migrations.c__DisplayClassc.b__b()+13
System.Data.Entity.Migrations.Dbmigator.EnsureDatabaseExists(操作 必须成功访问数据库)+422
System.Data.Entity.Migrations.DbMigrator.Update(字符串 targetMigration)+78


您的数据库中是否有相应的表?AspNetRoles/AspNetUsers等?是的,有这些表。我在服务器和本地都有数据。我已经更新了种子,想要更新服务器上的数据(部署)。看起来UserManager.Create失败了。失败后,您将尝试向不存在的Id添加角色。请检查UserManager.Create方法的IdentityResult以及它返回的错误类型。@misha130:同意。唯一可能导致此问题的原因是
UserManager.Create
失败,这意味着插入后,
Id
永远不会从数据库中回填。找出它失败的原因,然后你就可以参加比赛了。@misha130我发现我将我的表AspNetUsers重命名为users。请参阅上面的代码。这可能是问题所在?您的数据库中是否有相应的表?AspNetRoles/AspNetUsers等?是的,有这些表。我在服务器和本地都有数据。我已经更新了种子,想要更新服务器上的数据(部署)。看起来UserManager.Create失败了。失败后,您将尝试向不存在的Id添加角色。请检查UserManager.Create方法的IdentityResult以及它返回的错误类型。@misha130:同意。唯一可能导致此问题的原因是
UserManager.Create
失败,这意味着插入后,
Id
永远不会从数据库中回填。找出它失败的原因,然后你就可以参加比赛了。@misha130我发现我将我的表AspNetUsers重命名为users。请参阅上面的代码。这可能是问题所在?
// Change the name of the table to be Users instead of AspNetUsers
            modelBuilder.Entity<IdentityUser>()
                .ToTable("Users").Property(p => p.Id).HasColumnName("UserID");
            modelBuilder.Entity<ApplicationUser>()
                .ToTable("Users").Property(p => p.Id).HasColumnName("UserID");