【问题标题】:.Net Core: Unable to Scaffold Identity from Data Access Layer Context.Net Core:无法从数据访问层上下文中搭建身份
【发布时间】:2021-08-15 17:44:12
【问题描述】:

我正在开发一个用于学习目的的小型 .net core 3.1 应用程序,我有一个 Razor Pages 项目用作表示层 (UI)、一个用于域的链接库和一个用于数据访问的链接库以及一个用于业务层的链接库.

我正在尝试将核心身份集成到 DAL 中,我已成功创建表等,但我无法使用数据访问层中的数据库上下文构建身份,我的数据库上下文如下所示:

 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 搭建 Core Identity,但出现以下错误:

我已经尝试了几乎所有与包不匹配等相关的解决方案,我相信这与 DbContext 有关。

任何帮助都会很棒,提前感谢您。

【问题讨论】:

标签: c# .net asp.net-core


【解决方案1】:

您必须清除 NuGet 缓存。为此 ->

-> 转到工具 -> 选项 -> NuGet 包管理器 -> 一般 -> 清除所有 NuGet 缓存

如果不起作用,请检查您的所有 NuGet 包并确保您的 NuGet 包版本与您的 .net 核心相似。例如:-您使用的是 .net core 3.1,因此您的所有 NuGet 包管理器版本应该是 3.0/3.1/3.2 这样的。 version.特别是检查Microsoft.EntityFrameworkCore.SqlServerNuGet包版本,检查它是否与您的.net核心版本相似。Microsoft.EntityFrameworkCore.SqlServer这个包版本应该是3.0/3.1。如果没有,则升级或降级您的版本。之后再次清除 nuget 缓存,希望它能正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2020-01-24
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    相关资源
    最近更新 更多