在EF6中切换MySQL / SQL Server
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在EF6中切换MySQL / SQL Server相关的知识,希望对你有一定的参考价值。
我有一个在mysql和SQL Server上运行的项目。在过去的几周里,我已经设法使用基于现有SQL Server架构的数据库优先设计将其从普通旧SQL“迁移”到实体框架(也使用Spring依赖注入)。
现在我需要用MySQL进行测试。即使我使用了“适当的”MySQL配置,应用程序也会在从ClassCastException
到MySql.Data.MySqlClient.MySqlConnection
的System.Data.SqlClient.SqlConnection
上崩溃
码:
//Autowired by Spring
public auitool2014Entities DataContext{get;set;}
public IList<News> FindAllValid()
{
return (from News news in DataContext.news where news.annullata == 0 select news).ToList();
}
春季定义:
<object id="dataContext" singleton="false" scope="request" type="DiagnosticoSite.Models.auitool2014Entities, Auitool2014" factory-object="dataContextFactory" factory-method="Build" destroy-method="Dispose" ></object>
春天工厂:
public auitool2014Entities Build()
{
return new auitool2014Entities();
}
错误消息(从本地化翻译)
Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' to type 'System.Data.SqlClient.SqlConnection'.
跟踪堆栈跟踪:
[InvalidCastException: Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' type 'System.Data.SqlClient.SqlConnection'.]
System.Data.SqlClient.SqlCommand.set_DbConnection(DbConnection value) +26
System.Data.Common.DbCommand.set_Connection(DbConnection value) +9
System.Data.Entity.Internal.InterceptableDbCommand.set_DbConnection(DbConnection value) +41
System.Data.Common.DbCommand.set_Connection(DbConnection value) +9
System.Data.Entity.Core.Common.Utils.CommandHelper.SetStoreProviderCommandState(EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand) +123
System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.PrepareEntityCommandBeforeExecution(EntityCommand entityCommand) +314
System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +70
System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +1283
System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__6() +185
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +448
System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__5() +271
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +251
System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +648
System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +68
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() +68
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
DiagnosticoSite.Data.Managers.Spring.NewsManagerImpl.FindAllValid() in d:DocumentsVisual Studio 2013ProjectsAuitoolAuitool2014DataManagersSpringNewsManagerImpl.cs:16
配置完成后:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<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=6.9.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
我基本上将连接字符串更改为MySQL连接字符串
<add name="myConnectionString" connectionString="metadata=res://*/Models.Auitool2014.csdl|res://*/Models.Auitool2014.ssdl|res://*/Models.Auitool2014.msl;provider=Mysql.Data.MysqlClient;provider connection string="Server=localhost;Database=auitemp;Uid=root;Pwd=root;Allow Zero Datetime=True;CharSet=latin1;Pooling=True;Min Pool Size=5"" providerName="System.Data.EntityClient" />
如何在两个DB之间进行编译后进行切换?我提到Spring是因为我的DbContext
是request
-scoped对象,可以通过工厂对象轻松实例化。
调查进展
我发现堆栈跟踪报告了以下涉及的类:System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy
。这显然是SQL Server包的一部分。我需要了解EF如何实例化它。在EF源代码我see
var executionStrategy = ExecutionStrategy
?? DbProviderServices.GetExecutionStrategy(QueryState.ObjectContext.Connection, QueryState.ObjectContext.MetadataWorkspace);
真的,那必须是MySql.Data.entity.EF6.MySqlExecutionStrategy
的一个实例
固定更改实体框架qazxsw poi
configuration
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
是关键。总结一下,为了从SQL Server切换到MySQL,我必须:
- 编辑该死的连接字符串
Provider属性始终是
MySql.Data.Entity.MySqlEFConfiguration
在连接字符串中,将提供者更改为System.Data.EntityClient
- 用上面的替换
MySql.Data.MySqlClient
标签
以上是关于在EF6中切换MySQL / SQL Server的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 EF6 和 SQL Server 捕获 UniqueKey Violation 异常?
带有 EF6 的 WPF 应用程序不会在 SQL Server Enterprise 中创建新数据库
SQL Server to .Net Decimals with EF6 database first issue