Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

DbContextOptionsBuilder has no UseMySql method

ErrorUseMysql is not found on DbContextOptionsBuilder

Short answer

UseMySql comes from a provider package, not from EF Core itself. Install Pomelo.EntityFrameworkCore.MySql in the project that calls it, spell the method UseMySql with a capital S, and pass a server version. Match the package release to your EF Core version.

  • Applies to EF Core 9, Pomelo.EntityFrameworkCore.MySql 9.0.0, MySQL 8.4, .NET 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the packages in the project that contains the call. If no MySQL provider shows up, that is the answer.

    dotnet list package | findstr /i "mysql EntityFrameworkCore"
  2. 2Match what you see, then jump to its fix

    1. 1dotnet list package shows Microsoft.EntityFrameworkCore but nothing with MySql in the nameNo MySQL provider package is installed in that projectFix
    2. 2The compiler error says UseMysqlThe method name is spelled with the wrong capitalsFix
    3. 3The package restores, but versions of Microsoft.EntityFrameworkCore and the provider do not share a major numberThe Pomelo release does not match the EF Core versionFix

What does the error look like?

  • UseMysql is not found on DbContextOptionsBuilder
  • Cannot find the UseMysql method on DbContextOptions

Cause 1 of 3

No MySQL provider package is installed in that project

1Check

Microsoft documents database providers as separate NuGet packages that you install and then configure in the DbContext. For MySQL and MariaDB, its provider list names Pomelo.EntityFrameworkCore.MySql, and the Pomelo readme shows the UseMySql call.

dotnet list package

2Fix

Add the package to the project that registers the DbContext, then call UseMySql with a connection string and a server version.

dotnet add package Pomelo.EntityFrameworkCore.MySql

var serverVersion = ServerVersion.AutoDetect(connectionString);
services.AddDbContext<AppDbContext>(o => o.UseMySql(connectionString, serverVersion));

3Verify

The project builds, and the error under UseMySql disappears.

dotnet build

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

The method name is spelled with the wrong capitals

1Check

Pomelo's own sample calls the method UseMySql, with a capital S.

grep -rn "UseMysql" --include=*.cs .

2Fix

Rename the call to UseMySql. For MariaDB the Pomelo page says to use MariaDbServerVersion as the version object.

options.UseMySql(connectionString, new MariaDbServerVersion(new Version(10, 11, 0)));

3Verify

The build succeeds.

dotnet build

Confirmed by github.com

Did this fix it?

Cause 3 of 3

The Pomelo release does not match the EF Core version

1Check

Microsoft warns that providers typically do not work across major EF Core versions. The Pomelo page maps each release to one EF Core line: 9.0.0 to EF Core 9.0.x, 8.0.3 to 8.0.x, 7.0.0 to 7.0.x. It also lists the minimum MySqlConnector version for each.

dotnet list package --include-transitive | findstr /i "EntityFrameworkCore MySqlConnector"

2Fix

Move the provider to the release that belongs to your EF Core line, or move EF Core to the line the provider supports.

dotnet add package Pomelo.EntityFrameworkCore.MySql --version 9.0.0

3Verify

The restore finishes with no version warning and the build passes.

dotnet restore && dotnet build

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Put the provider package in the same project that calls AddDbContext.
  • Upgrade EF Core and the provider package in the same commit.
  • Read the compatibility table on the provider page before moving to a new .NET version.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.