升级到.NET 8:DbContext为空

问题描述 投票:0回答:1

我将 Razor 页面应用程序从 .NET 7 升级到 .NET 8。该应用程序在 .NET 7 中运行良好。在 .NET 8 中,

DbContext
为空。

我的应用程序中的相关部分:

ApplicationDbContext

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }

    public DbSet<Eigenaar> Eigenarens { get; set; }
    public DbSet<Object> Objectens { get; set; }
}

Program.cs

using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.EntityFrameworkCore;
using StallingRazor.Data;


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthentication(IISDefaults.AuthenticationScheme);
builder.Services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => false;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});
builder.Services.AddRazorPages(options =>
{
  //  options.RootDirectory = "/Stalling2";
    options.Conventions.AddPageRoute("/Eigenaren/Index", "");
});
builder.Services.AddSession();

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")
));
builder.Services.AddDistributedMemoryCache();
var app = builder.Build();

索引.cshtml:

        private readonly ApplicationDbContext _db;

        public EigenarenModel(ApplicationDbContext db)
        {
            _db = db;
        }
        private readonly ApplicationDbContext _db;
        public IEnumerable<Model.Eigenaar> Eigenaren { get; set; }

这是错误:

Text

任何关于为什么这在 .NET 7 中有效而在 .NET 8 中不起作用的提示都值得赞赏。

编辑:添加了 csproj:

  <ItemGroup>
    <PackageReference Include="JW.Pager" Version="1.0.1" />
    <PackageReference Include="MailKit" Version="4.3.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Identity.Client" Version="4.59.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="Select.HtmlToPdf.NetCore" Version="23.2.0" />
    <PackageReference Include="TinyMCE" Version="6.8.2" />
    <PackageReference Include="toastr" Version="2.1.1" />
  </ItemGroup>
asp.net entity-framework dbcontext
1个回答
0
投票

我使用 DbContext 创建了一个 .NET 7 项目进行测试。我使用 Upgrade Assistant 将其从 .NET 7 更新到 .NET 8,它对我来说效果很好。我认为您的错误可能是由于依赖项版本错误造成的。也许您可以尝试使用此工具重新更新。

如果此方法不起作用,它通常会指出错误的位置。然后您可以提供错误信息以获得进一步帮助。

© www.soinside.com 2019 - 2024. All rights reserved.