1

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
Microsoft.AspNet.Identity.UserManagerExtensions.AddToRole(UserManager
2 manager, TKey userId, String role) +119
xxx.Migrations.Configuration.Seed(ApplicationDbContext context) +292 System.Data.Entity.Migrations.DbMigrationsConfiguration1.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.<>c__DisplayClassc.b__b() +13
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +422
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +78

Anders
  • 375
  • 2
  • 5
  • 18
  • Do you have the appropriate tables in your database? AspNetRoles/AspNetUsers etc? – misha130 Dec 11 '15 at 14:26
  • Yes, have this tables. I have data in them on both the server and local. I have updated my seed and want to update the data on the server (deploy). – Anders Dec 11 '15 at 14:38
  • 1
    It seems like the UserManager.Create fails. After it Fails you are trying to add a role to a non existant Id. Check the IdentityResult of the UserManager.Create method and what kind of errors it gives back. – misha130 Dec 11 '15 at 14:42
  • @misha130: Agree. The only thing that could possible cause this problem is if `UserManager.Create` fails, which means that `Id` is never back-filled from the database after the insert. Find out why it's failing and you're off to the races. – Chris Pratt Dec 11 '15 at 14:51
  • @misha130 I found that I renamed my table AspNetUsers to users. See my code above. It may be the problem? – Anders Dec 11 '15 at 14:59
  • Yes Asp Identity follows a strict set of rules in which you have to define tables for things such as users/roles/claims. See that you override OnModelCreating like here: http://stackoverflow.com/questions/19460386/how-can-i-change-the-table-names-when-using-visual-studio-2013-aspnet-identity. But also look in to what UserManager.Create returns like mentioned above. – misha130 Dec 11 '15 at 15:06
  • Everything else works in the application like create user. How can I seed with my current User table? Looks like I override the same way as in your link. – Anders Dec 11 '15 at 15:34

0 Answers0