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

使用EF连接MariaDB遇版本不支持错误,无法执行EF操作

EF连接MariaDB执行EF操作失败问题

环境信息

  • 依赖库版本:MySql.Data v9.0.0、MySql.Data.EntityFramework v9.0.0
  • MariaDB版本:10.5.8-MariaDB-log

配置文件(app.config)

entityFramework节点

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="mssqllocaldb" />
    </parameters>
  </defaultConnectionFactory>
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=9.0.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
    </provider>
  </providers>
</entityFramework>

system.data节点

<system.data>
  <DbProviderFactories>
    <remove invariant="MySql.Data.MySqlClient" />
    <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=9.0.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
  </DbProviderFactories>
</system.data>

现象

  • 可正常打开/关闭数据库连接:
ctx.Database.Connection.Open();
ctx.Database.Connection.Close();
  • 可执行原生SQL查询:
ctx.Database.Connection.Open();
var command = ctx.Database.Connection.CreateCommand();
command.CommandText = "Select * from Device";
command.ExecuteNonQuery();
  • 执行EF LINQ操作(如ctx.ApplicationSettings.ToArray();)时抛出异常

异常信息

System.Data.Entity.Core.ProviderIncompatibleException: 访问数据库时出错。这通常意味着数据库连接失败。请检查连接字符串是否正确,以及是否正在使用适当的DbContext构造函数来指定它或在应用程序的配置文件中找到它。---> 
System.Data.Entity.Core.ProviderIncompatibleException: 提供程序未返回ProviderManifestToken字符串。---> 
System.NotSupportedException: 当前不支持5.6之前的MySQL版本
   在 MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection)
   在 System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
   --- 内部异常堆栈跟踪结束 ---
   在 System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
   在 System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
   --- 内部异常堆栈跟踪结束 ---
   在 System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
   在 System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.<>c__DisplayClass1_0.<ResolveManifestToken>b__0(Tuple`3 k)
   在 System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   在 System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection)
   在 System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
   在 System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   在 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   在 System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   在 System.Data.Entity.Internal.InternalContext.Initialize()
   在 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   在 System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   在 System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   在 System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()

问题原因

MySql.Data.EntityFramework v9.0.0的数据库提供程序在识别MariaDB版本时存在兼容性问题,错误地将MariaDB 10.5.8识别为低于5.6的MySQL版本,导致EF无法获取有效的ProviderManifestToken,进而无法执行EF相关操作。

解决方案

方案1:升级MySQL EF提供程序版本

  1. 卸载当前NuGet包:MySql.Data v9.0.0、MySql.Data.EntityFramework v9.0.0
  2. 安装匹配版本的新版包(建议安装v8.0.x系列,如v8.0.33,确保两个包版本完全一致)
  3. 更新app.config中对应的版本号:
    • 修改entityFramework节点下provider的Version为安装的版本
    • 修改system.data节点下DbProviderFactories的Version为安装的版本

方案2:在连接字符串中强制指定服务器版本

在数据库连接字符串中添加ServerVersion参数,指定兼容的版本号,例如:

server=localhost;database=你的数据库名;uid=用户名;pwd=密码;ServerVersion=10.5.8-MariaDB

或指定兼容的MySQL版本号(如ServerVersion=5.7),绕过提供程序的版本检测逻辑。

方案3:自定义ProviderManifestToken解析(进阶)

继承MySqlProviderServices并重写GetDbProviderManifestToken方法,手动返回正确的版本号,但该方案需要编写额外代码,仅推荐前两种方案无法解决时使用。

验证

修改完成后,重新执行EF LINQ操作(如ctx.ApplicationSettings.ToArray();),确认是否正常执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 02:15:02