Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'MySQLDbContextOptionsBuilder' does not contain a definition for 'ServerVersion'

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);
            }));
} 
like image 543
Roohi Avatar asked May 13 '20 14:05

Roohi


1 Answers

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))
    )
);
like image 70
retif Avatar answered Oct 24 '22 07:10

retif