我在我的项目中应用了 ASP.NET Core 3.1,我想通过代码优先的方法创建数据库并使用 MySQL。
在 startup.cs 文件中,我收到此错误:
CS1061 'MySQLDbContextOptionsBuilder' does not contain a definition for 'ServerVersion' and no accessible extension method 'ServerVersion' accepting a first argument of type 'MySQLDbContextOptionsBuilder'
我该如何解决?
在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);
}));
}
最佳答案
不确定你有什么版本的 Pomelo 包,但肯定有 breaking changes那里(另请参阅此 issue ),所以现在您需要以不同的方式对其进行初始化。
例如,如果您希望它自动检测版本:
//_connectionString = _configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<IdentityContext>(
options => options.UseMySql(
_connectionString,
ServerVersion.AutoDetect(_connectionString)
)
);
或者如果你想设置一个特定的版本:services.AddDbContext<IdentityContext>(
options => options.UseMySql(
_configuration.GetConnectionString("DefaultConnection"),
new MySqlServerVersion(new Version(8, 0, 21))
)
);
关于asp.net-core - 'MySQLDbContextOptionsBuilder' 不包含 'ServerVersion' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61777554/