我正在尝试设置一个新的asp .net core 3项目。我正在尝试将.net身份添加到其中。尝试创建数据库迁移时,出现以下错误;
访问Microsoft.Extensions.Hosting服务时发生错误。没有应用程序服务提供商就继续进行。错误:“ Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore`6 [TUser,TContext,TKey,TUserClaim,TUserLogin,TUserToken]上的GenericArguments [0],“ Microsoft.AspNetCore.Identity.IdentityUser”违反了类型“ TUser'。
我整个下午都在努力解决这个问题,但是我无法弄清楚这意味着什么,更重要的是,如何解决它。
我的程序类如下:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
我的Startup.cs文件如下:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<McClureDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<McClureDbContext>();
services.AddRazorPages();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// User settings.
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
我的DbContext类看起来像这样:
public class McClureDbContext : Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
public McClureDbContext()
{
}
public McClureDbContext(DbContextOptions<McClureDbContext> options): base(options)
{
}
public DbSet<Home> Homes { get; set; }
public DbSet<HomePicture> HomePictures { get; set; }
public DbSet<HomeVideo> HomeVideos { get; set; }
public DbSet<ApplicationUser> AspNetUsers { get; set; }
public DbSet<Province> Province { get; set; }
public DbSet<Country> Country { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
var builder = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var Configuration = builder.Build();
var connString = Configuration.GetConnectionString("DefaultConnection");
optionsBuilder.UseSqlServer(connString, options => options.EnableRetryOnFailure());
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Home>().Property(n => n.Sold).HasDefaultValue(false);
}
}
我目前的三种数据类型如下:
public class Home
{
public Home()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int32 HomeId { get; set; }
public Guid AspNetUsersId { get; set; }
[ForeignKey("AspNetUsersId")]
public ApplicationUser ApplicationUser { get; set; }
[MaxLength(100)]
public string Address1 { get; set; }
[MaxLength(100)]
public string Address2 { get; set; }
[MaxLength(100)]
public string City { get; set; }
public Int64 ProvinceId { get; set; }
[ForeignKey("ProvinceId")]
public Province Province { get; set; }
[MaxLength(20)]
public string PostalCode { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public bool Sold { get; set; }
public ICollection<HomeVideo> HomeVideos { get; set; }
public ICollection<HomePicture> HomePictures { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateEntered { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateUpdated { get; set; }
}
public class HomeVideo
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int32 HomeVideoId { get; set; }
public Int32 HomeId { get; set; }
[ForeignKey("HomeId")]
public Home Home { get; set; }
[MaxLength(500)]
public string Description { get; set; }
[MaxLength(1024)]
public string VideoUrl { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateEntered { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateUpdated { get; set; }
}
public class HomePicture
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int32 HomePictureId { get; set; }
public Int32 HomeId { get; set; }
[ForeignKey("HomeId")]
public Home Home { get; set; }
[MaxLength(500)]
public string Description { get; set; }
[MaxLength(1024)]
public string PictureUrl { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateEntered { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime DateUpdated { get; set; }
}
当我在asp .net core 2.x中构建东西时,我不记得遇到任何这类问题。我认为自己做错了事,但不知道如何解决。搜索谷歌今天没有太大的帮助。我很茫然。任何想法/方向都表示赞赏。
TIA, 沃利