You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core 3.1 Startup.cs中MySqlServerVersion报CS0246错误求助

解决.NET Core 3.1中Pomelo EF Core MySQL的CS0246错误

问题详情

在代码行new MySqlServerVersion(new Version(8, 0, 40))处遇到CS0246错误(找不到类型或命名空间),同时编辑器中以下两个命名空间引用显示为灰色(未被使用):

using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Pomelo.EntityFrameworkCore.MySql.Storage;

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");
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

环境信息

  • .NET Core 3.1(严格指定版本)
  • Pomelo.EntityFrameworkCore.MySql 3.2.7
  • 三层架构(Core、DAL、Web层),Startup.cs位于Web层

已尝试的解决方法

  • 重新安装对应版本的包,并确认包已在Web层的依赖项中
  • 多次执行dotnet clean、dotnet build、dotnet restore命令

解决步骤

  1. 修正命名空间引用
    移除未被使用的灰色命名空间,添加Pomelo EF Core的核心命名空间:

    // 移除以下两行
    // using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
    // using Pomelo.EntityFrameworkCore.MySql.Storage;
    
    // 添加正确的命名空间
    using Pomelo.EntityFrameworkCore.MySql;
    
  2. 确认包引用配置
    检查Web层项目的.csproj文件,确保Pomelo包引用正确且版本匹配:

    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.7" />
    

    同时确保项目中其他EF Core相关包的版本与.NET Core 3.1兼容(需为EF Core 3.1.x版本)。

  3. 调整ServerVersion初始化代码
    在Pomelo 3.2.7版本中,MySqlServerVersion属于Pomelo.EntityFrameworkCore.MySql命名空间,修正后的DbContext配置代码如下:

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseMySql(
            Configuration.GetConnectionString("DefaultConnection"),
            new MySqlServerVersion(new Version(8, 0, 40))
        )
    );
    

    也可以使用自动检测MySQL版本的方式简化配置:

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseMySql(
            Configuration.GetConnectionString("DefaultConnection"),
            ServerVersion.AutoDetect(Configuration.GetConnectionString("DefaultConnection"))
        )
    );
    
  4. 重新清理构建项目
    执行以下命令彻底清理项目缓存并重新构建:

    dotnet clean
    dotnet restore
    dotnet build
    

内容的提问来源于stack exchange,提问作者Trusha Jadeja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:05:55