- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
(这曾经是一个由两部分组成的问题,但由于第二部分确实很重要,所以我决定将其分成两个单独的帖子。请参阅 Using Serialization to copy entities between two ObjectContexts in Entity Framework 了解第二部分。
我想为我的实体模型创建一个相当通用的数据库“克隆器”。此外,我可能需要支持不同的提供商等。我正在使用 ObjectContext
API。
我知道 this question already和 EntityConnectionStringBuilder MDSN documentation例如,但我需要知道是否有一种编程方式来获取值以初始化 EntityConnectionStringBuilder
的 Provider
和 Metadata
属性?
using (var sourceContext = new EntityContext()) {
var sourceConnection = (EntityConnection) sourceContext.Connection;
var targetConnectionBuilder = new EntityConnectionStringBuilder();
targetConnectionBuilder.ProviderConnectionString = GetTargetConnectionString();
targetConnectionBuilder.Provider = "System.Data.SqlClient"; // want code
targetConnectionBuilder.Metadata = "res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl"; // want code
using (var targetContext = new EntityContext(targetConnectionBuilder.ConnectionString)) {
if (!targetContext.DatabaseExists())
targetContext.CreateDatabase();
// how to copy all data from the source DB to the target DB???
}
}
也就是有没有办法获取
“System.Data.SqlClient”
"res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl"
来自某处而不使用文字值?
最佳答案
您应该能够使用 res://*/
告诉 Entity Framework 在调用程序集中搜索所有 .csdl、.ssdl 和 .msl 文件。或者,使用 res://assembly full name here/
在特定程序集中搜索。请注意,这两种语法都将加载所有找到的文件,直到您在同一个程序集中有多个 .edmx,这会正常工作,从而产生多个 CSDL/SSDL/MSL 文件(.edmx 文件基本上是这三个文件的串联)。更多信息 on MSDN .
如果您想要更多控制,请使用 Assembly.GetManifestResourceNames列出给定程序集中的所有资源,并手动将 .csdl/.ssdl/.msl 资源匹配在一起,然后从这些资源名称手动构建元数据字符串。
可以在根节点的 Provider 属性中的 SSDL 文件中找到提供程序。获得正确的文件名后,使用 GetManifestResourceStream
并将文件读取为 XML。代码应如下所示:
using (var stream = assembly.GetManifestResourceStream("EntityModel.ssdl")) {
XDocument document = XDocument.Load(stream);
string provider = document.Root.Attribute("Provider").Value;
}
关于c# - "Cloning" Entity Framework 中的 EntityConnections 和 ObjectContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589017/
我有一个关于 EntityFramework with CodeFirst 方法的问题。基于 EntityConnection 源代码和文档,我无法使用已打开的 SqlConnection 创建它。它
关于管理 EntityContext 生命周期有很多问题, 例如Instantiating a context in LINQ to Entities 我得出的结论是,实体上下文应被视为一个工作单元,
“EntityConnection 只能用关闭的 DbConnection 构造”这是我在尝试构建提供开放连接的实体连接时遇到的问题。有一个 transactionscope 打开,我不想打开一个新连
我有一个使用 EF CTP5 的应用。 在这种特殊情况下,我需要降级到一些经典的 ADO.NET(以便在存储过程中读取多个结果集,这是 EF 不支持的)。 因此,我尝试使用 EntityConnect
例如: SqlConnection connection = new SqlConnection(efContext.Connection...?) connection.Open(); etc..
出于集成测试目的,我想在测试夹具设置中手动创建并打开 EntityConnection。调用 Open() 方法时失败并出现以下异常: System.Data.ProviderIncompatible
创建 EF4 EntityConnection 的成本如何?我正在使用 SQL Compact 创建一个 EF4 桌面应用程序,用户将能够使用“文件打开”对话框打开数据库文件。然后我的代码构建一个 E
我在使用 EntityConnection 时收到此错误,我不知道如何解决。使用 ObjectContext 时,我可以设置属性 DefaultContainerName 并且它可以工作,但现在我不知
(这曾经是一个由两部分组成的问题,但由于第二部分确实很重要,所以我决定将其分成两个单独的帖子。请参阅 Using Serialization to copy entities between two
我的大部分数据库操作都使用 Linq2Entity。然而,对于数据库创建、表创建和初始数据插入,我使用纯 SQL 文件。因此我需要一个 SqlConnection 和一个 EntityConnecti
我是一名优秀的程序员,十分优秀!