Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cloning" EntityConnections and ObjectContexts in Entity Framework

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

like image 985
Fernando Espinosa Avatar asked Nov 13 '22 21:11

Fernando Espinosa


1 Answers

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;
}
like image 162
Julien Lebosquain Avatar answered Nov 15 '22 12:11

Julien Lebosquain