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

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

问题描述

(这曾经是一个2部分的问题,但由于第二部分是字面上的重要一员,我决定把它分成两个独立的帖子查看的使用序列化到实体框架复制两个ObjectContexts间实体的第二部分。

(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.

我知道这一问题已经并在<一个href="http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.aspx"相对=nofollow> EntityConnectionStringBuilder MDSN文档例子,但我需要知道,如果有一个编程的方式来获取值初始化提供的属性 EntityConnectionStringBuilder

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 |高分辨率://*/EntityModel.ssd​​l |高分辨率://*/EntityModel.msl
  • "System.Data.SqlClient"
  • "res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl"

使用文字值?

推荐答案

您应该可以使用水库:// * / 来告诉实体框架来搜索所有.csdl,.ssdl和.msl文件调用程序集。另外,使用 RES://在这里组装的全名/ 在一个特定的组件进行搜索。需要注意的是这两种语法将加载的所有找到的文件,直到你有几个的.edmx同一程序,导致一些CSDL / SSDL / MSL文件,工作正常(一个.edmx文件基本上是这三个文件的串联)。更多信息 href="http://msdn.microsoft.com/en-us/library/cc716756.aspx" rel="nofollow">。

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.

如果你想要更多的控制,使用<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcenames.aspx"相对=nofollow> 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.

提供商可以在根节点的提供者属性的SSDL文件中找到。一旦你有了正确的文件名,使用 GetManifestResourceStream 并读取该文件的XML。在code应该是这样的:

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

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

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