【发布时间】:2012-10-12 17:57:13
【问题描述】:
我需要在一个应用程序中合并
- 编译前从 DB 生成的代码到 EDMX 文件
- 应用程序本身在运行时生成和编译的代码,其中生成的代码使用 CodeFirst 访问 DB。
备注:1.和2.中的代码有不同的DbContexts,访问同一个数据库,但不同的表。
看起来,当我在不同的事务范围内使用类型 1. 和 2. 的实例时,一切正常。但是当我尝试在一个事务范围内一起使用它们时,我得到了错误(如果首先调用 EDMX)
System.Reflection.TargetInvocationException: Exception has been thrown by the ta
rget of an invocation. ---> System.Data.ProviderIncompatibleException: An error
occurred while getting provider information from the database. This can be cause
d by Entity Framework using an incorrect connection string. Check the inner exce
ptions for details and ensure that the connection string is correct. ---> System
.Data.ProviderIncompatibleException: **The provider did not return a ProviderManif
estToken string.** ---> System.Transactions.TransactionException: **The operation is
not valid for the state of the transaction.**
和错误(如果首先使用 CodeFirst)
System.Data.MetadataException: Schema specified is not valid. Errors:
(0,0) : error 0175: **The specified store provider cannot be found in the configur
ation, or is not valid.**
at System.Data.Metadata.Edm.StoreItemCollection.Loader.ThrowOnNonWarningErrors()
为了使我的情况更加复杂,我必须添加以下内容:描述的行为只有在数据库位于远程服务器上时才会出现。如果我使用本地数据库,一切看起来都很好。我的怀疑是分布式事务协调器可以发挥它的作用......
主要问题:是否可以在一个 TransactionScope 中结合 EDMX 和 CodeFirst。如果是那怎么办?
任何帮助将不胜感激。米洛斯
【问题讨论】:
-
您使用的是什么数据库以及配置文件的外观。
-
数据库:MS SQL Server 2008 CodeFirst 连接字符串:@"Server=nyx; Database=Workflow3; Trusted_Connection=true; Enlist=True;" EDMX 连接字符串:metadata=res://AppFrameworkLibrary/AppFrameworkLibrary.csdl|res://AppFrameworkLibrary/AppFrameworkLibrary.ssdl|res://AppFrameworkLibrary/AppFrameworkLibrary.msl;provider=System.Data.SqlClient;provider 连接string="Data Source=nyx;Initial Catalog=Workflow3;Integrated Security=True;MultipleActiveResultSets=True;Enlist=True;"
-
使用 codefirst,您不应该既没有 EDMX 也没有实体连接字符串。 CodeFirst 根据您的类在内部创建模型(即 edmx)。
标签: c# entity-framework ef-code-first msdtc