asp.net-core - 'MySQLDbContextOptionsBuilder' 不包含 'ServerVersion' 的定义

标签 asp.net-core entity-framework-core dbcontext asp.net-core-3.0

我在我的项目中应用了 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/

相关文章:

c# - 托管 Blazor WASM GetFromJsonAsync : The JSON value could not be converted to System. Collections.Generic.IEnumerable`

c# - 将 DBContext 与依赖注入(inject)结合使用的后果

c# - HealthChecks.UI.SqlServer.Storage 迁移问题

razor - 如何使用没有 Controller 的 Razor Pages 填充下拉列表

asp.net-core - 使用新的.Net Core 3.0 grpc服务进行集成测试的需求指南

c# - 如何刷新 DbContext

c# - 如何让 ASP.NET Core 2.0 MVC Model Binder 绑定(bind)没有 FromBody 属性的复杂类型

c# - 如何按角色的排序顺序对员工列表进行排序?

c# - Entity Framework 核心,更新数据库不考虑属性