Hi Kmcnet,
Thank you for your response. I’ve updated the solution based on your answer.
- Kill namespace/alias collisions in the failing controller
Remove any aliases or wrong usings:
// BAD (causes EF6 or custom DbContext to be used)
using System.Data.Entity;
using DbContext = Some.Other.Namespace.DbContext;
using myDbContext = Some.Other.Namespace.MyDbContext;
Explicitly use your EF Core context
using YourApp.Data; // where myDbContext is declared
using var ctx = new YourApp.Data.myDbContext();
- Align EF Core 9 with .NET 9 and keep provider versions in lockstep In
.csproj:
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.9" />
</ItemGroup>
Then:
dotnet clean
dotnet restore --force
dotnet build
dotnet list package
Ensure no EF6 (EntityFramework) sneaked in.
- Delete bin/obj and NuGet caches to fix stale type resolution
dotnet nuget locals all --clear
rm -rf bin obj # (Windows: rd /s /q bin && rd /s /q obj)
dotnet restore
dotnet build
- Prefer DI over new myDbContext() to avoid mismatches
// Program.cs / Startup.cs
services.AddDbContext<myDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Default")));
// Controller
public class FooController : Controller
{
private readonly myDbContext _ctx;
public FooController(myDbContext ctx) => _ctx = ctx;
}
If #1 fixes it, you had a namespace/alias conflict. If #2 fixes it, you had a framework/package mismatch. If #3 fixes it, it was cache corruption. If you still see the error after these, rename the context to a unique name (e.g., AppDbContext) to break collisions across the solution.