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

.NET 9 Blazor连接MySQL时无法创建DbContext求助

.NET 9 Blazor 连接 MySQL 8.4.3 迁移失败问题

尝试用.NET 9 Blazor连接MySQL 8.4.3数据库,查阅资料并尝试多种包后仍未成功,执行迁移命令时出现错误。

已安装的NuGet包

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
</ItemGroup>

appsettings.json 配置

"ConnectionStrings": {
    "DefaultConnection": "Server=x.x.x.x;Database=DB_Cours;User=UserCours;Password=12345678"
},

Program.cs 配置代码

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");

builder.Services.AddDbContext<MySqlDbContext>(options =>
    options.UseMySql(connectionString, new MySqlServerVersion(new Version(8,4,3))));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

MySqlDbContext 代码

public class MySqlDbContext(DbContextOptions<MySqlDbContext> options) : DbContext(options)
{
    public DbSet<Person>? Person { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Person>().HasData(
            new Person { Id = 1, Name = "Laurent"},
            new Person { Id = 2, Name = "John"},
            new Person { Id = 3, Name = "Mike" },
            new Person { Id = 4, Name = "Larry" }
        );
    }
}

执行迁移时的错误信息

Unable to create a 'DbContext' of type 'RuntimeType'.

The exception 'Method 'get_LockReleaseBehavior' in type 'Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlHistoryRepository' from assembly 'Pomelo.EntityFrameworkCore.MySql, Version=8.0.2.0, Culture=neutral, PublicKeyToken=2cc498582444921b' does not have an implementation.' was thrown while attempting to create an instance.

解决方案

问题根源

错误是因为Pomelo.EntityFrameworkCore.MySql 8.0.2 不兼容 EF Core 9.0(.NET 9),EF Core 9新增了LockReleaseBehavior属性,旧版本Pomelo未实现该方法。

修复步骤

  1. 卸载当前的Pomelo包:
    Uninstall-Package Pomelo.EntityFrameworkCore.MySql
    
  2. 安装兼容EF Core 9的Pomelo版本(需与EF Core版本匹配):
    Install-Package Pomelo.EntityFrameworkCore.MySql -Version 9.0.0
    
  3. 确保所有EF Core相关包版本统一为9.0.0(当前配置已满足)
  4. 重新执行迁移命令:
    Add-Migration Init
    

额外建议

MySQL 8.0+默认要求SSL连接,可在连接字符串中添加SslMode=Required避免连接失败:

"DefaultConnection": "Server=x.x.x.x;Database=DB_Cours;User=UserCours;Password=12345678;SslMode=Required"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 10:58:21