Nugget 17: Override a Global EF Tool Locally with an Earlier Version

Problem

You need to create an Entity Framework migration for a project that targets an earlier version of .NET. After updating your models, you run:

dotnet dotnet-ef migrations add UpdateStudent

The command throws an exception:

Build started...

Build succeeded.
Unhandled exception. System.IO.FileLoadException:
Could not load file or assembly 'System.Runtime, Version=8.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

The located assembly's manifest definition does not match the assembly reference.
(0x80131040)

File name: 'System.Runtime, Version=8.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

The issue is that the globally installed dotnet-ef command-line tool is version 8.x or 9.x. When you execute dotnet ef migrations add, the global tool runs within your project's context. Because the tool itself requires .NET 8, it attempts to load assemblies that do not exist in a .NET 5 application and fails with a runtime exception.

Your project is fine—the command-line tool is simply too new for the target framework.

The Fix

Instead of downgrading your globally installed EF tools, configure a repository-specific version using a local tool manifest.

The following approach is adapted from a Stack Overflow solution and tailored to this scenario.

1. Create a local tool manifest file

dotnet new tool-manifest

2. Install a .NET 5-compatible version of the EF tool locally

dotnet tool install --local dotnet-ef --version 5.0.17

3. Run your migration using the local Tool Override


dotnet dotnet-ef migrations add UpdateStudent

Because the project now contains a local tool manifest, .NET will use the repository-specific version of dotnet-ef rather than the globally installed version.


Future Commands

Whenever you need to create migrations or update the database in this .NET 5 project, use the local tool:

dotnet dotnet-ef migrations add <MigrationName>

dotnet dotnet-ef database update

This keeps the project isolated from changes to globally installed tooling and avoids version compatibility issues when working with legacy applications.


References



Comments

Popular posts from this blog

Nugget 11: Adding Filtering to a Blazor QuickGrid

Nugget 9: Building a JobBank App using Blazor. A Nugget with a Story.

Nugget 14: Connection to MongoDB Atlas - SSL Alert 80