How can I create a MetadataWorkspace using metadata loading delegates?

☆樱花仙子☆ 提交于 2019-12-04 21:47:46

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

    }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!