Asp.net mvc 5 id列已更改的种子标识用户表?

Asp.net mvc 5 id列已更改的种子标识用户表?,asp.net-mvc-5,asp.net-identity,usermanager,Asp.net Mvc 5,Asp.net Identity,Usermanager,我已将AspNetUser(identity)选项卡更改为仅具有以下代码的用户 // Change the name of the table to be Users instead of AspNetUsers modelBuilder.Entity<IdentityUser>() .ToTable("Users").Property(p => p.Id).HasColumnName("UserID");

我已将AspNetUser(identity)选项卡更改为仅具有以下代码的用户

        // 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");
//将表的名称更改为Users而不是AspNetUsers
modelBuilder.Entity()
.ToTable(“Users”).Property(p=>p.Id).HasColumnName(“UserID”);
modelBuilder.Entity()
.ToTable(“Users”).Property(p=>p.Id).HasColumnName(“UserID”);
我的申请很好,但不是我的种子。我在Migrations/Configuration.cs中有此代码

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

        context.SaveChanges();
var UserManager=newusermanager(newuserstore(context));
var角色管理器=新角色管理器(新角色存储(上下文));
字符串role=“Admin”;
字符串密码=”Password@1234";
//如果角色Admin不存在,则创建该角色
如果(!RoleManager.RoleExists(角色))
{
var roleresult=rolemanger.Create(新的IdentityRole(角色));
}
//创建密码为123456的管理员用户
var admin1=新的ApplicationUser();
admin1.UserName=”admin1@admin1.com";
admin1.Email=”admin1@admin1.com";
admin1.emailconfirm=true;
创建(admin1,密码);
AddToRole(admin1.Id,角色);
SaveChanges();
我收到错误消息“找不到UserId”。似乎是我的用户管理器。创建失败


如何更改种子代码以使用用户Id而不是标准Id?

实际上,当您保存用户时,它还没有给定Id,您必须在创建用户和addToRole之间检索用户:

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

    // Refetch user with ID:
    dbAdmin1 = context.Users.FirstOrDefault(x => x.UserName == admin1.UserName);

    UserManager.AddToRole(dbAdmin1.Id, role);

    context.SaveChanges();
var UserManager=newusermanager(newuserstore(context));
var角色管理器=新角色管理器(新角色存储(上下文));
字符串role=“Admin”;
字符串密码=”Password@1234";
//如果角色Admin不存在,则创建该角色
如果(!RoleManager.RoleExists(角色))
{
var roleresult=rolemanger.Create(新的IdentityRole(角色));
}
//创建密码为123456的管理员用户
var admin1=新的ApplicationUser();
admin1.UserName=”admin1@admin1.com";
admin1.Email=”admin1@admin1.com";
admin1.emailconfirm=true;
创建(admin1,密码);
//重新蚀刻ID为的用户:
dbAdmin1=context.Users.FirstOrDefault(x=>x.UserName==admin1.UserName);
AddToRole(dbAdmin1.Id,角色);
SaveChanges();