Upgraded to .NET 8: DbContext is null

159 views Asked by At

I upgraded my Razor pages app from .NET 7 to .NET 8. The app was working fine in .NET 7. In .NET 8, the DbContext is null.

Relevant pieces in my app:

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; }

And this is the error:

Text

Any hint on why this worked in .NET 7 and does not work in .NET 8 is appreciated.

Edit: Added the 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>
1

There are 1 answers

3
Ivan On

I created a .NET 7 project with DbContext for testing. I used the Upgrade Assistant to update it from .NET 7 to .NET 8, and it worked fine for me. I think your error might be caused by having the wrong versions of dependencies. Perhaps you could try re-updating using this tool.

If this approach doesn't work, it typically points to the location of the error. You could then provide the error information for further assistance.