未找到UserId,在迁移种子中添加用户和角色

时间:2015-12-11 14:23:04

标签: 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 = 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();           
    }

我收到错误消息“UserId not found”如果我在PM中运行“update-database”或使用复选框“Execute code first migration”进行部署。

修改 我已将用户表重命名为AspNetUsersUsers,请参阅以下IdentityModels中的代码

// 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");

一些跟踪日志

  

[InvalidOperationException:找不到UserId。]
  Microsoft.AspNet.Identity.d__83.MoveNext()+1279
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+93
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+52 Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func 1 func) +266
Microsoft.AspNet.Identity.UserManagerExtensions.AddToRole(UserManager
2   manager,TKey userId,String role)+119
  xxx.Migrations.Configuration.Seed(ApplicationDbContext context)   +292 System.Data.Entity.Migrations.DbMigrationsConfiguration 1.OnSeed(DbContext context) +42
System.Data.Entity.Migrations.DbMigrator.SeedDatabase() +102
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
1   pendingMigrations,String targetMigrationId,String lastMigrationId)   +448 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)+446
  System.Data.Entity.Migrations。&lt;&gt; c__DisplayClassc.b__b()+ 13
  System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(动作   mustSucceedToKeepDatabase)+422
  System.Data.Entity.Migrations.DbMigrator.Update(字符串   targetMigration)+78

0 个答案:

没有答案