I've applied ASP.NET Core 3.1 in my project and I want to create database by code-first approach and use MySQL. In startup.cs file I got this error:
CS1061 'MySQLDbContextOptionsBuilder' does not contain a definition for 'ServerVersion' and no accessible extension method 'ServerVersion' accepting a first argument of type 'MySQLDbContextOptionsBuilder'
How can I solve it?
in startup.cs:
using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<Alpha.Web.App.ApplicationDbContext>(options =>
options.UseMySQL(Configuration["ConnectionStrings:DefaultConnection"],
mysqlOptions =>
{
mysqlOptions.ServerVersion(new Version(8, 0, 20), ServerType.MySql);
}));
}
Not sure what version of Pomelo package you have, but there were certain breaking changes there (see also this issue), so now you need to initialize it a bit differently.
For example, if you want it to auto-detect the version:
//_connectionString = _configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<IdentityContext>(
options => options.UseMySql(
_connectionString,
ServerVersion.AutoDetect(_connectionString)
)
);
or if you want to set a specific version:
//_connectionString = _configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<IdentityContext>(
options => options.UseMySql(
_connectionString,
new MySqlServerVersion(new Version(8, 0, 21))
)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With