Entity Framework Code Stopped Working

Kmcnet 1,236 Reputation points
2025-10-04T15:32:34.34+00:00

Hello everyone and thanks for the help in advance. I have a Visual Studio 2022 project that utilizes Entity Framework. I last opened the project 3 days ago and the project worked fine. Today, I opened the project and immediately was met with several errors type used in a using statement must implement System.IDisposable. The code throwing the error:

            using (var ctx = new myDbContext())
            {

            }

which is used multiple times throughout the project, but only throws the error within one controller. There is another error in myDbContext:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

throws no suitable method found to override. These errors happened without any changes to the project. I tried cleaning the project, but that did not work. Any help would be appreciated.

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Tran (WICLOUD CORPORATION) 350 Reputation points Microsoft External Staff
    2025-10-06T04:24:31.7633333+00:00

    Hi Kmcnet,

    Thank you for your response. I’ve updated the solution based on your answer.

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

    1. 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
    
    1. 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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.