.NET Core 2.2应用程序出现问题。我向我的应用程序添加了身份验证和用户实体,现在我的应用程序在启动时引发异常。当我尝试添加迁移时,会引发另一个异常。
当我尝试添加初始迁移时:
无法添加实体类型“ AppUser”的种子实体,因为 为属性“ Id”提供的值不是“ int”类型。 考虑使用'DbContextOptionsBuilder.EnableSensitiveDataLogging'来 查看涉及的属性值。
当我尝试启动我的应用程序时:
无法添加实体类型“ AppUser”的种子实体,因为 为物业提供的值'95ee39c2-a865-4272-8b67-da590fb4b80c' “ Id”不是“ 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");
}
}
有人知道我在做什么错吗?以及如何正确配置身份和种子管理员用户?
答案 0 :(得分:1)
确保您的ResumeContext
继承自使用IdentityDbContext
的{{1}}。
您可以在此处找到如何通过基于IdentityUser<int>
的{{1}}配置自定义IdentityDbContext
的示例:Your Identity based DB context should look like this