How can I create a MetadataWorkspace using metadata loading delegates?

database-schema entityconnection entity-framework-6 runtime

Question

I followed this example Changing schema name on runtime - Entity Framework where I can create a new EntityConnection from a MetaDataWorkspace that I then use to construct a DbContext with a different schema, but I get compiler warnings saying that RegisterItemCollection method is obsolete and to "Construct MetadataWorkspace using constructor that accepts metadata loading delegates."

How do I do that? Here is the code that is working but gives the 3 warnings for the RegsiterItemCollection calls. I'm surprised it works since warning says obsolete not just deprecated.

        public static EntityConnection CreateEntityConnection(string schema, string connString, string model)
    {
        XmlReader[] conceptualReader = new XmlReader[]
        {
            XmlReader.Create(
                Assembly
                    .GetExecutingAssembly()
                    .GetManifestResourceStream(model + ".csdl")
            )
        };

        XmlReader[] mappingReader = new XmlReader[]
        {
            XmlReader.Create(
                Assembly
                    .GetExecutingAssembly()
                    .GetManifestResourceStream(model + ".msl")
            )
        };

        var storageReader = XmlReader.Create(
            Assembly
                .GetExecutingAssembly()
                .GetManifestResourceStream(model + ".ssdl")
        );
        //XNamespace storageNS = "http://schemas.microsoft.com/ado/2009/02/edm/ssdl"; // this would not work!!!
        XNamespace storageNS = "http://schemas.microsoft.com/ado/2009/11/edm/ssdl";

        var storageXml = XElement.Load(storageReader);

        foreach (var entitySet in storageXml.Descendants(storageNS + "EntitySet"))
        {
            var schemaAttribute = entitySet.Attributes("Schema").FirstOrDefault();
            if (schemaAttribute != null)
            {
                schemaAttribute.SetValue(schema);
            }
        }
        storageXml.CreateReader();

        StoreItemCollection storageCollection =
            new StoreItemCollection(
                new XmlReader[] { storageXml.CreateReader() }
            );
        EdmItemCollection conceptualCollection = new EdmItemCollection(conceptualReader);
        StorageMappingItemCollection mappingCollection =
            new StorageMappingItemCollection(
                conceptualCollection, storageCollection, mappingReader
            );

        //var workspace2 = new MetadataWorkspace(conceptualCollection, storageCollection, mappingCollection);
        var workspace = new MetadataWorkspace();
        workspace.RegisterItemCollection(conceptualCollection);
        workspace.RegisterItemCollection(storageCollection);
        workspace.RegisterItemCollection(mappingCollection);

        var connectionData = new EntityConnectionStringBuilder(connString);
        var connection = DbProviderFactories
            .GetFactory(connectionData.Provider)
            .CreateConnection();
        connection.ConnectionString = connectionData.ProviderConnectionString;

        return new EntityConnection(workspace, connection);
    }
1
0
5/23/2017 11:51:12 AM

Accepted Answer

I was able to get rid of the 3 warning messages. Basically it wants you to register the collections in the constructor of the MetadataWorkspace.

There are 3 different overloads for MetadataWorkspace, I chose to use the one which requires to to supply a path (array of strings) to the workspace metadata. To do this I saved readers to temp files and reloaded them.

This is working for me without any warnings.

public static EntityConnection CreateEntityConnection(string schema, string connString, string model) {

        var conceptualReader = XmlReader.Create(Assembly.GetExecutingAssembly().GetManifestResourceStream(model + ".csdl"));
        var mappingReader = XmlReader.Create(Assembly.GetExecutingAssembly().GetManifestResourceStream(model + ".msl"));
        var storageReader = XmlReader.Create(Assembly.GetExecutingAssembly().GetManifestResourceStream(model + ".ssdl"));

        XNamespace storageNS = "http://schemas.microsoft.com/ado/2009/11/edm/ssdl";

        var storageXml = XElement.Load(storageReader);
        var conceptualXml = XElement.Load(conceptualReader);
        var mappingXml = XElement.Load(mappingReader);

        foreach (var entitySet in storageXml.Descendants(storageNS + "EntitySet")) {
            var schemaAttribute = entitySet.Attributes("Schema").FirstOrDefault();
            if (schemaAttribute != null) {
                schemaAttribute.SetValue(schema);
            }
        }

        storageXml.Save("temp.ssdl");
        conceptualXml.Save("temp.csdl");
        mappingXml.Save("temp.msl");

        MetadataWorkspace workspace = new MetadataWorkspace(new List<String>(){
                                                                @"temp.csdl",
                                                                @"temp.ssdl",
                                                                @"temp.msl"
                                                        } 
                                                       ,  new List<Assembly>());


        var connectionData = new EntityConnectionStringBuilder(connString);
        var connection = DbProviderFactories.GetFactory(connectionData.Provider).CreateConnection();
        connection.ConnectionString = connectionData.ProviderConnectionString;

        return new EntityConnection(workspace, connection);

    }
3
9/26/2014 1:18:56 PM

Popular Answer

Not wanting to create temp files which slows the process down, I found an alternate answer to this is fairly simple. I replaced these lines of code -

    //var workspace2 = new MetadataWorkspace(conceptualCollection, storageCollection, mappingCollection);
    var workspace = new MetadataWorkspace();
    workspace.RegisterItemCollection(conceptualCollection);
    workspace.RegisterItemCollection(storageCollection);
    workspace.RegisterItemCollection(mappingCollection);

with this one line of code -

    var workspace = new MetadataWorkspace(() => conceptualCollection, () => storageCollection, () => mappingCollection);

and that works fine.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow