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

问题描述

(这以前是一个2部分的问题,但由于第二部分是重要的一个问题,所以我决定把它分成两个单独的帖子。请参阅第二部分。



我想为我的实体模型创建一个相当通用的cloner数据库,另外,我可能需要支持不同的提供程序,我使用 ObjectContext API。 >

我知道,而示例,但我需要知道是否有获取价值的程序化方式来初始化 Provider 元数据属性n 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; //要代码

using(var targetContext = new EntityContext(targetConnectionBuilder.ConnectionString)){
if(!targetContext.DatabaseExists())
targetContext.CreateDatabase();

//如何将源数据库中的所有数据复制到目标数据库?
}
}

就是说,有没有办法获取




  • System.Data.SqlClient

  • RES://*/EntityModel.csdl | RES://*/EntityModel.ssdl | RES://*/EntityModel.msl



从某处不使用文字值?

解决方案

h3>元数据

您应该可以使用 res:// * / 来告诉实体框架来搜索调用程序集中的所有.csdl,.ssdl和.msl文件。或者,使用 res://这里的程序集全名/ 在特定程序集中进行搜索。请注意,这两种语法都将加载所有找到的文件,这样才能正常工作,直到在同一程序集中有几个.edmx,导致了几个CSDL / SSDL / MSL文件(一个.edmx文件基本上是这三个文件的连接)。更多信息。



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



提供者



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

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


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

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.

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?

解决方案

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.

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 15:20