You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7升级至.NET 8后Razor Pages中DbContext为null问题求助

.NET 7升级至.NET 8后Razor Pages中DbContext为null的问题分析

问题概述

将Razor Pages应用从.NET 7升级至.NET 8后,原本运行正常的应用出现ApplicationDbContext实例为null的问题。

相关代码片段

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();

Index.cshtml模型代码

private readonly ApplicationDbContext _db;

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

项目文件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>

错误信息

DbContext为null错误

可能的原因分析

1. Razor Page模型未继承PageModel

Razor Pages的模型类必须继承PageModel基类,才能让依赖注入容器正确注入服务。你的EigenarenModel代码中未显示继承PageModel,.NET 7可能对该规则检查较宽松,而.NET 8严格执行了该要求,导致DbContext无法被注入。

修复方式:确保模型类继承PageModel:

public class EigenarenModel : PageModel
{
    private readonly ApplicationDbContext _db;

    public EigenarenModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public IEnumerable<Model.Eigenaar> Eigenaren { get; set; }
}

2. 重复定义私有字段

在EigenarenModel中重复定义了private readonly ApplicationDbContext _db;,这会导致编译器或DI容器混淆,无法正确赋值。.NET 7可能忽略了该重复定义,而.NET 8的编译器或DI逻辑更严格,导致注入失败。

修复方式:删除重复的字段定义,仅保留一处。

3. 服务注册顺序错误

在Program.cs中,AddSession()的调用早于AddDistributedMemoryCache()。Session服务依赖分布式缓存,.NET 8对服务依赖的顺序检查更严格,错误的注册顺序可能导致DI容器初始化异常,间接影响DbContext的注入。

修复方式:调整注册顺序,先注册分布式缓存再注册Session:

builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession();

4. 第三方包兼容性问题

部分第三方包(如JW.Pager版本1.0.1)可能未适配.NET 8,导致依赖注入容器出现异常,影响DbContext的正常注入。

修复方式:检查所有第三方包是否有.NET 8兼容的版本,优先更新到最新适配版本。

内容的提问来源于stack exchange,提问作者leof

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 05:22:41