I have this seed in 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();
}
I get the error message "UserId not found" If I in PM run "update-database" or doing a deploy with the checkbox "Execute code first migration" checked.
EDIT
I have renamed my user table to from AspNetUsers
to Users
, see code below in 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");
Some trace log
[InvalidOperationException: UserId not found.]
Microsoft.AspNet.Identity.d__83.MoveNext() +1279
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func1 func) +266
2 manager, TKey userId, String role) +119
Microsoft.AspNet.Identity.UserManagerExtensions.AddToRole(UserManager
xxx.Migrations.Configuration.Seed(ApplicationDbContext context) +292 System.Data.Entity.Migrations.DbMigrationsConfiguration1.OnSeed(DbContext context) +42
1 pendingMigrations, String targetMigrationId, String lastMigrationId) +448 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +446
System.Data.Entity.Migrations.DbMigrator.SeedDatabase() +102
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
System.Data.Entity.Migrations.<>c__DisplayClassc.b__b() +13
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +422
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +78