.NET Core 2.2 - 种子管理员用户
.NET Core 2.2 - Seed admin users
我的 .NET Core 2.2 应用程序有问题。我向我的应用程序添加了身份验证和用户实体,现在我的应用程序在启动时抛出异常。当我尝试添加迁移时,抛出了另一个异常。
当我尝试添加初始迁移时:
The seed entity for entity type 'AppUser' cannot be added because the
value provided for the property 'Id' is not of the type 'int'.
Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to
see the involved property values.
当我尝试启动我的应用程序时:
The seed entity for entity type 'AppUser' cannot be added because the
value '95ee39c2-a865-4272-8b67-da590fb4b80c' provided for the property
'Id' is not of the type 'int'.
我不知道我做错了什么。我试着改变了几个小时但没有成功。
Startup.cs -- ConfigureServices 方法
services.AddDbContext<ResumeContext>(options =>
options
.UseSqlServer(Configuration.GetConnectionString("ResumeConnection"))
.EnableSensitiveDataLogging()
);
var builder = services.AddIdentityCore<AppUser>(o =>
{
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredUniqueChars = 0;
o.Password.RequiredLength = 6;
});
builder = new IdentityBuilder(builder.UserType, typeof(AppRole), builder.Services);
builder.AddEntityFrameworkStores<ResumeContext>().AddDefaultTokenProviders();
SeedData.cs
public static class SeedData
{
public static IWebHost SeedAdminUser(this IWebHost webHost)
{
using (var scope = webHost.Services.CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<AppUser>>();
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<AppRole>>();
if (userManager.Users.Any(u => u.Email == "admin@domain.com")) return webHost;
roleManager.CreateAsync(new AppRole { Name = AppRole.Admin }).Wait();
userManager.CreateAsync(new AppUser { UserName = "Admin", Email = "admin@domain.com" }, "CCZtE2XpqXbSs5B").Wait();
userManager.AddToRoleAsync(userManager.FindByEmailAsync("admin@domain.com").Result, AppRole.Admin).Wait();
}
return webHost;
}
}
AppUser.cs
public class AppUser : IdentityUser<int>
{
// TODO: This will be extended in future
}
User.cs
public class User
{
public int Id { get; set; }
public int AppUserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string PostalCode { get; set; }
public DateTime CreationDate { get; set; }
public DateTime? ModifyDate { get; set; }
public long? FacebookId { get; set; }
public AppUser AppUser { get; set; }
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build()
.SeedAdminUser()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Context.cs
public class ResumeContext : IdentityDbContext<AppUser, AppRole, int>
{
public ResumeContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configure("Resume.Infrastructure.Data");
}
}
有人知道我做错了什么吗?以及如何正确配置身份和种子管理员用户?
确保您的 ResumeContext
继承自使用 IdentityUser<int>
的 IdentityDbContext
。
您可以在此处找到如何使用基于 int
IDs
配置自定义 IdentityDbContext
的示例:
我的 .NET Core 2.2 应用程序有问题。我向我的应用程序添加了身份验证和用户实体,现在我的应用程序在启动时抛出异常。当我尝试添加迁移时,抛出了另一个异常。
当我尝试添加初始迁移时:
The seed entity for entity type 'AppUser' cannot be added because the value provided for the property 'Id' is not of the type 'int'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.
当我尝试启动我的应用程序时:
The seed entity for entity type 'AppUser' cannot be added because the value '95ee39c2-a865-4272-8b67-da590fb4b80c' provided for the property 'Id' is not of the type 'int'.
我不知道我做错了什么。我试着改变了几个小时但没有成功。
Startup.cs -- ConfigureServices 方法
services.AddDbContext<ResumeContext>(options =>
options
.UseSqlServer(Configuration.GetConnectionString("ResumeConnection"))
.EnableSensitiveDataLogging()
);
var builder = services.AddIdentityCore<AppUser>(o =>
{
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredUniqueChars = 0;
o.Password.RequiredLength = 6;
});
builder = new IdentityBuilder(builder.UserType, typeof(AppRole), builder.Services);
builder.AddEntityFrameworkStores<ResumeContext>().AddDefaultTokenProviders();
SeedData.cs
public static class SeedData
{
public static IWebHost SeedAdminUser(this IWebHost webHost)
{
using (var scope = webHost.Services.CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<AppUser>>();
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<AppRole>>();
if (userManager.Users.Any(u => u.Email == "admin@domain.com")) return webHost;
roleManager.CreateAsync(new AppRole { Name = AppRole.Admin }).Wait();
userManager.CreateAsync(new AppUser { UserName = "Admin", Email = "admin@domain.com" }, "CCZtE2XpqXbSs5B").Wait();
userManager.AddToRoleAsync(userManager.FindByEmailAsync("admin@domain.com").Result, AppRole.Admin).Wait();
}
return webHost;
}
}
AppUser.cs
public class AppUser : IdentityUser<int>
{
// TODO: This will be extended in future
}
User.cs
public class User
{
public int Id { get; set; }
public int AppUserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string PostalCode { get; set; }
public DateTime CreationDate { get; set; }
public DateTime? ModifyDate { get; set; }
public long? FacebookId { get; set; }
public AppUser AppUser { get; set; }
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build()
.SeedAdminUser()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Context.cs
public class ResumeContext : IdentityDbContext<AppUser, AppRole, int>
{
public ResumeContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configure("Resume.Infrastructure.Data");
}
}
有人知道我做错了什么吗?以及如何正确配置身份和种子管理员用户?
确保您的 ResumeContext
继承自使用 IdentityUser<int>
的 IdentityDbContext
。
您可以在此处找到如何使用基于 int
IDs
配置自定义 IdentityDbContext
的示例: