“克隆"实体框架中的 EntityConnections 和 ObjectContexts [英] "Cloning" EntityConnections and ObjectContexts in Entity Framework

查看:24
本文介绍了“克隆"实体框架中的 EntityConnections 和 ObjectContexts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这曾经是一个由两部分组成的问题,但由于第二部分实际上很重要,我决定将其拆分为两个单独的帖子.参见 第二部分使用序列化在实体框架中的两个 ObjectContext 之间复制实体.

(This used to be a 2-part question, but since the second part is literally the important one, I decided to split this into two separate posts. See Using Serialization to copy entities between two ObjectContexts in Entity Framework for the second part.

我想为我的实体模型创建一个相当通用的数据库克隆器".此外,我可能需要支持不同的提供商等.我正在使用 ObjectContext API.

I want to create a fairly generic "cloner" of databases for my entity model. Also, I might need to support different providers and such. I'm using ObjectContext API.

我已经知道这个问题EntityConnectionStringBuilder MDSN 文档 示例,但我需要知道是否有编程方式来获取要初始化的值EntityConnectionStringBuilderProviderMetadata 属性?

I am aware of this question already and the EntityConnectionStringBuilder MDSN documentation example, but I need to know if there is a programmatic way to obtain the values to initialize the Provider and Metadata properties of an EntityConnectionStringBuilder?

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???
    }
}

也就是说,有没有办法获取

That is, is there a way to fetch the

  • "System.Data.SqlClient"
  • "res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl"

来自某处而不使用文字值?

from somewhere and not use literal values?

推荐答案

元数据

您应该能够使用 res://*/ 告诉实体框架在调用程序集中搜索所有 .csdl、.ssdl 和 .msl 文件.或者,使用 res://assembly full name here/ 在特定程序集中进行搜索.请注意,这两种语法都将加载所有找到的文件,在同一个程序集中有多个 .edmx 之前,这会正常工作,从而产生多个 CSDL/SSDL/MSL 文件(一个 .edmx 文件基本上是这三个文件的串联).更多信息MSDN.

Metadata

You should be able to use res://*/ to tell Entity Framework to search for all .csdl, .ssdl and .msl files in the calling assembly. Alternatively, use res://assembly full name here/ to search in a specific assembly. Note that both these syntaxes will load all found files, which works fine until you have several .edmx in the same assembly, resulting in several CSDL/SSDL/MSL files (an .edmx file is basically a concatenation of those three files). More information on MSDN.

如果您想要更多控制,请使用 Assembly.GetManifestResourceNames 列出给定程序集中的所有资源,并将 .csdl/.ssdl/.msl 资源手动匹配在一起,然后根据这些资源名称手动构建元数据字符串.

If you want more control, use Assembly.GetManifestResourceNames to list all resources in a given assembly, and match the .csdl/.ssdl/.msl resources manually together, then build your metadata string manually from those resource names.

提供者可以在根节点的Provider属性中的SSDL文件中找到.获得正确的文件名后,请使用 GetManifestResourceStream 并将文件读取为 XML.代码应如下所示:

The provider can be found in the SSDL file in the Provider attribute of the root node. Once you have the correct file name, Use GetManifestResourceStream and read the file as XML. The code should look like this:

using (var stream = assembly.GetManifestResourceStream("EntityModel.ssdl")) {
  XDocument document = XDocument.Load(stream);
  string provider = document.Root.Attribute("Provider").Value;
}

这篇关于“克隆"实体框架中的 EntityConnections 和 ObjectContexts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆