Cloning EntityConnections and ObjectContexts in the Entity Framework

(This was a two-part question, but since the second part is literally important, I decided to split it into two separate posts. See Using Serialization to copy objects between two object-oriented objects in the Entity Framework for the second part.

I want to create a fairly general database cloner for my entity model. In addition, I may need the support of various suppliers, etc. I am using the API ObjectContext.

I already know this question and the example documentation of EntityConnectionStringBuilder MDSN , but I need to know if there is a programmatic way to get the values ​​to initialize the properties Providerand Metadatafor 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, there is a way to get

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

from somewhere and not use literal values?

+5
source share
1 answer

Metadata

res://*/, Entity Framework .csdl,.ssdl .msl . res://assembly full name here/ . , , , .edmx , CSDL/SSDL/MSL ( .edmx ). MSDN.

, Assembly.GetManifestResourceNames, .csdl/.ssdl/.msl , .

Provider

SSDL Provider node. GetManifestResourceStream XML. :

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

All Articles