我正在开发一个用于学习的小型.net核心3.1应用程序,我有一个剃刀页面项目用作表示层(UI),一个用于域的链接库和一个用于数据访问的链接库,以及一个用于业务层的链接库。
我正在尝试集成DAL中的核心身份,我已经成功地创建了表等,但我无法使用我的数据访问层中的DB上下文构建身份,我的数据库上下文如下所示:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=DESKTOP-9CTCUHB;
Database= xyz;
Integrated Security= True");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Business>()
.HasOne(e => e.BusinessProfile)
.WithOne(b => b.Business)
.HasForeignKey<BusinessProfile>(e => e.BusinessId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Business>()
.HasMany(e => e.Bookings)
.WithOne(b => b.Business)
.HasForeignKey(e => e.BusinessID)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Business>()
.HasMany(e => e.Serivces)
.WithOne(b => b.Business)
.HasForeignKey(e => e.BusinessID)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<AppUser>()
.HasOne(e => e.Business)
.WithOne(b => b.BusinessOwnerUser)
.HasForeignKey<Business>(b => b.BusinessOwnerUserId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Business>()
.HasOne(e => e.Type)
.WithOne(b => b.Business)
.HasForeignKey<BusinessType>(e => e.BusinessID)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Business>()
.HasOne(e => e.BusinessOwnerUser)
.WithOne(b => b.Business)
.HasForeignKey<AppUser>(e => e.BusinessID)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Booking>()
.HasMany(e => e.ServiceBooking)
.WithOne(b => b.Booking)
.HasForeignKey( e => e.BookingID)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Service>()
.HasMany(e => e.ServiceBooking)
.WithOne(b => b.Service)
.HasForeignKey( e => e.ServiceID)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<PaymentInfo>()
.HasOne(e => e.PaymentMethod)
.WithOne(b => b.Payment)
.HasForeignKey<PaymentMethod>(e => e.PaymendInfoID)
.OnDelete(DeleteBehavior.NoAction);我的RazorUI初创版看起来像这样
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MLContext>();
services.AddRazorPages();
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<MLContext>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}我正在使用MLContext搭建核心身份,但得到以下错误:

我尝试了几乎所有与包不匹配等相关的解决方案,我相信这与DbContext有关。
任何帮助都将是很棒的,并提前感谢你。
发布于 2021-08-16 07:05:40
您必须清除NuGet缓存。要执行此操作,->
->转至工具->选项-> NuGet包管理器->常规->清除所有NuGet缓存
如果不能工作,那么检查您的所有Nuget包,并确保您的NuGet包版本与您的.net核心相似。例如:-您使用的是.net Core3.1,因此您的所有NuGet包管理器版本应该是3.0/ 3.1 /3.2。version.especially检查Microsoft.EntityFrameworkCore.SqlServerNuGet包版本,检查是否与您的.net核心版本相似。Microsoft.EntityFrameworkCore.SqlServer此包的版本应为3.0/3.1。如果不是,则升级或降级您的版本。之后,再次清除nuget缓存,希望它能正常工作。
https://stackoverflow.com/questions/68793993
复制相似问题