Startup.cs 中.NET Core 3.1 中的 MySqlServerVersion 错误

问题描述 投票:0回答:1

我遇到错误:CS0246 行

new MySqlServerVersion(new Version(8, 0, 40)) 

以下是我的

Startup.cs

using Enterprise.EmployeeManagement.DAL.Data;
using Enterprise.EmployeeManagement.Core.Services;
using Microsoft.EntityFrameworkCore;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Microsoft.Extensions.Hosting;
using Pomelo.EntityFrameworkCore.MySql.Storage;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // Configure Entity Framework to use MySQL with Pomelo and MySqlConnector
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseMySql(
                Configuration.GetConnectionString("DefaultConnection"),
                new MySqlServerVersion(new Version(8, 0, 40))  
            )
        );

        // Register the UserService as scoped
        services.AddScoped<UserService>();

        // Add controllers with views
        services.AddControllersWithViews();
    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
           https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

以下软件包在编辑器中似乎已禁用/灰色:

using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Pomelo.EntityFrameworkCore.MySql.Storage;
  • 我正在使用.NET Core 3.1(完全相同的版本),Pomelo.EntityFrameworkCore.MySql 3.2.7
  • 使用具有核心层、DAL 层和 Web 层的三层架构(Startup.cs 存在于 WEb 层)

我尝试做以下事情:

  • 尝试重新安装所需版本的软件包,并确保它们位于 Web 层的依赖项/软件包中
  • 尝试过
    dotnet clean
     dotnet build
    dotnet resotre
    多次。
c# mysql-connector pomelo-entityframeworkcore-mysql
1个回答
0
投票

在旧版本(3.2.7)中,

MySqlServerVersion
可能无法直接工作。您可以将服务器版本指定为字符串:

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
mySqlOptions => mySqlOptions.ServerVersion("8.0.40-mysql"))            
    );
© www.soinside.com 2019 - 2024. All rights reserved.