【问题标题】:Azure MySQL Entity Framework Infrastructure errorAzure MySQL 实体框架基础结构错误
【发布时间】:2018-10-02 22:18:24
【问题描述】:

我刚刚使用 Entity Framework 和 MySQL 构建了一个应用程序。在 localhost 中一切正常,但是当我发布应用程序并在服务器中运行时,出现错误:

    System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.InvalidCastException: [A]MySql.Data.MySqlClient.MySqlTransaction cannot be cast to [B]MySql.Data.MySqlClient.MySqlTransaction. Type A originates from 'MySql.Data, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' in the context 'Default' at location 'D:\Windows\Microsoft.Net\assembly\GAC_MSIL\MySql.Data\v4.0_6.5.4.0__c5687fc88969c44d\MySql.Data.dll'. Type B originates from 'MySql.Data, Version=6.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' in the context 'Default' at location 'D:\local\Temporary ASP.NET Files\root\491e3a19\118d844c\assembly\dl3\ce9fb3e1\00af7a53_1f2ed401\MySql.Data.dll'.
   at MySql.Data.Entity.EFMySqlCommand.set_DbTransaction(DbTransaction value)
   at System.Data.Common.DbCommand.set_Transaction(DbTransaction value)
   at System.Data.Entity.Internal.InterceptableDbCommand.set_DbTransaction(DbTransaction value)
   at System.Data.Common.DbCommand.set_Transaction(DbTransaction value)
   at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
   at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
   at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
   at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at TazWorksAPI.Views.ViewModel.CreateRequest.createPackageByName(RequestModel model)

它是否说我有 2 个版本的 MySQL?我该如何解决?

谢谢

【问题讨论】:

  • 它说你有库不匹配。某些库(即 EF)尝试使用版本为 6.5.4.0 的库(存在于服务器的全局程序集缓存中)第二个库(即您的代码)尝试使用位于您的应用程序文件夹中的版本 6.10.8.0。这可能意味着,一个库对版本的要求较低,并且不存在于您的本地文件夹中(否则加载优先级将采用本地优先级而不是来自 GAC),而第二个库具有您复制的 dll 范围。您可能需要将MySql.Data.dll 复制到更靠近库的位置,这会导致问题。
  • 安装的 MySQL 连接器是 6.5.4。我没有 6.10.8 的任何东西
  • Type B originates from 'MySql.Data, Version=6.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' in the context 'Default' at location 'D:\local\Temporary ASP.NET Files\root\491e3a19\118d844c\assembly\dl3\ce9fb3e1\00af7a53_1f2ed401\MySql.Data.dll' 这可能来自您的应用程序。然后看看发布包中创建了什么。
  • 我将 MySQL.Data 更新为正确的版本,然后我得到:没有为具有不变名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序找到实体框架提供程序。确保提供程序已在应用程序配置文件的“entityFramework”部分注册
  • @MaxBoy 请不要在 cmets 中问后续问题!

标签: .net entity-framework azure mysql-connector


【解决方案1】:

请查看以下文档:ASP.NET Identity: Using MySQL Storage with an EntityFramework MySQL Provider (C#)

有一段:Making project configuration changes to the Web.config file for your application

请确保从那时起已遵循所有步骤。而且,正如 WueF 之前提到的:“请注意不要使用 MySQL.Data v.6.10.4。一个简单的降级到 6.9.10 解决了我数周调查我找不到的问题。”

【讨论】: