UseMySql in Pomelo throwing exception

2.6k Views Asked by At

So I have seen the strangest behavior with the Pomelo package for .NET Core.

var optionsBuilder = new DbContextOptionsBuilder<AssessmentContext>();
var cn = String.Format(config["ConnectionStrings:AwsConnection"], config["Aws:MySQLPassword"]);

optionsBuilder.UseMySql(cn);
db = new AssessmentContext(optionsBuilder.Options);

I've had the above code in unit tests to set up connect to a MySQL database in AWS. This has worked just fine for about a week now. However, starting today, when it gets to optionsBuilder.UseMySql(cn); now I'm getting an exception.

Assessment.Tests.Data.Contexts.AssessmentContextTests.ConnectToMySQL:
    Outcome: Failed
    Error Message:
    Class Initialization method Assessment.Tests.Data.Contexts.AssessmentContextTests.Initialize threw exception. System.TypeLoadException: System.TypeLoadException: Could not load type 'MySql.Data.MySqlClient.MySqlConnectionStringBuilder' from assembly 'MySqlConnector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d33d3e53aa5f8c92'..
    Stack Trace:
       at Microsoft.EntityFrameworkCore.MySqlDbContextOptionsExtensions.UseMySql(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 mySqlOptionsAction)
   at Microsoft.EntityFrameworkCore.MySqlDbContextOptionsExtensions.UseMySql[TContext](DbContextOptionsBuilder`1 optionsBuilder, String connectionString, Action`1 mySqlOptionsAction)
   at Assessment.Tests.Data.Contexts.AssessmentContextTests.Initialize(TestContext context) in

This has been working fine. I haven't updated any libraries, but it's failing and I'm unable to connect. Any thoughts? I'm using .NET Core 3.1, and pomelo version 3.0.1.

1

There are 1 best solutions below

1
On BEST ANSWER

It's a version mismatch. MySqlConnector changed its namespace in version 1.0.0. Since this is a breaking change and Pomelo is SemVer2 compliant, Pomelo is still using MySqlConnector < 1.0.0 (currently 0.69.9).

Later versions of Pomelo (e.g. the current version 3.2.2) explicitly restrict the MySqlConnector version to below 1.0.0, so that using a MySqlConnection version >= 1.0.0 would lead to a compile time error instead of a runtime error.

So to fix the issue, use the latest version of MySqlConnector below 1.0.0.


On a side note, you should consider using EF Core 3.1.8 and Pomelo 3.2.2. There have been many bugfixes for Pomelo since version 3.0.1.