C# 如何在ASP.NET Identity中使用OnModelCreating添加管理员用户?

C# 如何在ASP.NET Identity中使用OnModelCreating添加管理员用户?,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,如何使用ASP.NET Identity中的OnModelCreating添加管理员用户 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<IdentityUser> ( user.UserName = "admin"; user.Email = "admin@gmail.com"; string

如何使用ASP.NET Identity中的
OnModelCreating
添加管理员用户

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUser>
    (
       user.UserName = "admin";
       user.Email = "admin@gmail.com";
       string userPassword = "Admin123#";
    ).ToTable("AspNetUsers");
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity
(
user.UserName=“admin”;
user.Email=”admin@gmail.com";
字符串userPassword=“Admin123#”;
).ToTable(“AspNetUsers”);
}
如何在ASP.NET Identity中使用OnModelCreating添加管理员用户

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUser>
    (
       user.UserName = "admin";
       user.Email = "admin@gmail.com";
       string userPassword = "Admin123#";
    ).ToTable("AspNetUsers");
}
您不能使用此方法执行此操作

OnModelCreating的文档说明如下:

通常,在创建派生上下文的第一个实例时,此方法只调用一次

这适用于创建上下文时,而不是创建上下文上任何定义的类型化dbset时

如果您想在对象发送到数据库之前对其进行更改,则其他帖子也会给出该答案

如何在ASP.NET Identity中使用OnModelCreating添加管理员用户

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUser>
    (
       user.UserName = "admin";
       user.Email = "admin@gmail.com";
       string userPassword = "Admin123#";
    ).ToTable("AspNetUsers");
}
您不能使用此方法执行此操作

OnModelCreating的文档说明如下:

通常,在创建派生上下文的第一个实例时,此方法只调用一次

这适用于创建上下文时,而不是创建上下文上任何定义的类型化dbset时

如果您想在对象发送到数据库之前对其进行更改,则其他帖子也会给出该答案

试试这个:

 protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // any unique string id
        const string ADMIN_ID = "a18be9c0-aa65-4af8-bd17-00bd9344e575";
        const string ROLE_ID = "ad376a8f-9eab-4bb9-9fca-30b01540f445";

        builder.Entity<IdentityRole>().HasData(new IdentityRole
        {
            Id = ROLE_ID,
            Name = "admin",
            NormalizedName = "admin"
        });

        var hasher = new PasswordHasher<IdentityUser>();
        builder.Entity<IdentityUser>().HasData(new IdentityUser
        {
            Id = ADMIN_ID,
            UserName = "admin",
            NormalizedUserName = "admin",
            Email = "admin@gmail.com",
            NormalizedEmail = "admin@gmail.com",
            EmailConfirmed = false,
            PasswordHash = hasher.HashPassword(null, "Admin123#"),
            SecurityStamp = string.Empty
        });

        builder.Entity<IdentityUserRole<string>>().HasData(new IdentityUserRole<string>
        {
            RoleId = ROLE_ID,
            UserId = ADMIN_ID
        });
    }
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
//任何唯一的字符串id
常量字符串管理_ID=“a18be9c0-aa65-4af8-bd17-00bd9344e575”;
const string ROLE_ID=“ad376a8f-9eab-4bb9-9fca-30b01540f445”;
builder.Entity().HasData(新的IdentityRole
{
Id=角色\u Id,
Name=“admin”,
NormalizedName=“admin”
});
var hasher=new PasswordHasher();
builder.Entity().HasData(新标识用户
{
Id=管理员Id,
UserName=“admin”,
NormalizedUserName=“admin”,
电子邮件=”admin@gmail.com",
标准化邮件=”admin@gmail.com",
emailconfirm=false,
PasswordHash=hasher.HashPassword(null,“Admin123#”),
SecurityStamp=string.Empty
});
builder.Entity().HasData(新的IdentityUserRole
{
RoleId=角色ID,
UserId=ADMIN\u ID
});
}
参考:

试试这个:

 protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // any unique string id
        const string ADMIN_ID = "a18be9c0-aa65-4af8-bd17-00bd9344e575";
        const string ROLE_ID = "ad376a8f-9eab-4bb9-9fca-30b01540f445";

        builder.Entity<IdentityRole>().HasData(new IdentityRole
        {
            Id = ROLE_ID,
            Name = "admin",
            NormalizedName = "admin"
        });

        var hasher = new PasswordHasher<IdentityUser>();
        builder.Entity<IdentityUser>().HasData(new IdentityUser
        {
            Id = ADMIN_ID,
            UserName = "admin",
            NormalizedUserName = "admin",
            Email = "admin@gmail.com",
            NormalizedEmail = "admin@gmail.com",
            EmailConfirmed = false,
            PasswordHash = hasher.HashPassword(null, "Admin123#"),
            SecurityStamp = string.Empty
        });

        builder.Entity<IdentityUserRole<string>>().HasData(new IdentityUserRole<string>
        {
            RoleId = ROLE_ID,
            UserId = ADMIN_ID
        });
    }
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
//任何唯一的字符串id
常量字符串管理_ID=“a18be9c0-aa65-4af8-bd17-00bd9344e575”;
const string ROLE_ID=“ad376a8f-9eab-4bb9-9fca-30b01540f445”;
builder.Entity().HasData(新的IdentityRole
{
Id=角色\u Id,
Name=“admin”,
NormalizedName=“admin”
});
var hasher=new PasswordHasher();
builder.Entity().HasData(新标识用户
{
Id=管理员Id,
UserName=“admin”,
NormalizedUserName=“admin”,
电子邮件=”admin@gmail.com",
标准化邮件=”admin@gmail.com",
emailconfirm=false,
PasswordHash=hasher.HashPassword(null,“Admin123#”),
SecurityStamp=string.Empty
});
builder.Entity().HasData(新的IdentityUserRole
{
RoleId=角色ID,
UserId=ADMIN\u ID
});
}
参考:


u必须提供所有必填字段su必须提供所有必填字段