zoukankan      html  css  js  c++  java
  • 无法加载指定的元数据资源Unable to load the specified metadata resource

    在使用ADO.NET Entity Framework构建Application时出现如下异常。Solution 中有2个项目,一个为Windows Form项目,另一个为Class Library 项目,包含ADO.NET Entity Data Model。Windows Form 项目增加对Class Library 项目的引用。
    异常信息:
    System.Data.MetadataException was unhandled
    Message="Unable to load the specified metadata resource."
    Source="System.Data.Entity"
    StackTrace:
           at System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
           at System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
           at System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
           at System.Data.EntityClient.EntityConnection.SplitPaths(String paths)
           at System.Data.EntityClient.EntityConnection.GetMetadataWorkspace(Boolean initializeAllCollections)
           at System.Data.EntityClient.EntityConnection.InitializeMetadata(DbConnection newConnection, DbConnection originalConnection, Boolean closeOriginalConnectionOnFailure)
           at System.Data.EntityClient.EntityConnection.Open()
    在Windows Form 项目中出现异常的代码:
                string customerID = txtCustomerID.Text.Trim();
                // Contains a reference to an Entity Data Model (EDM) and a data source connection.
                using (EntityConnection cn = new EntityConnection("Name=NorthwindEntities"))
                {
                    cn.Open();
                    EntityCommand cmd = cn.CreateCommand();
                    cmd.CommandText =
                         "SELECT VALUE c FROM NorthwindEntities.Customers "+
                         "AS c WHERE c.CustomerID = @customerID";
                    cmd.Parameters.AddWithValue("customerID", customerID);
                    DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                    while (rdr.Read())
                        Console.WriteLine(rdr["CompanyName"].ToString());
                    rdr.Close();
                }
    上述代码行 cn.Open(); 抛出异常Exception - Unable to load the specified metadata resource。经检查,问题出现在App.config 配置文件(该配置文件在使用ADO.NET Entity Data Model向导时自动添加),
    ADO.NET Entity Data Model向导自动添加的Connection String:
    <configuration>
    <connectionStrings>
        <add name="NorthwindEntities" connectionString="metadata=res://*/NorthwindDB.csdl|res://*/NorthwindDB.ssdl|res://*/NorthwindDB.msl; provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True&quot;"providerName="System.Data.EntityClient" />
    </connectionStrings>
    </configuration>
    需要将上面connectionStrings 配置节中的* 修改为Class Library 项目的Assembly Name。修改后的示例App.config 如下:
    <configuration>
    <connectionStrings>
        <add name="NorthwindEntities" connectionString=" metadata= res://NorthwindEDM/NorthwindModel.csdl|res://NorthwindEDM/NorthwindModel.ssdl|res://NorthwindEDM/NorthwindModel.msl; provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True&quot;"providerName="System.Data.EntityClient" />
    </connectionStrings>
    </configuration
        The problem sometimes occurs with the Entity Framework where it gives an array of errors when doing simple operations. For example, I received an error about not being able to “unable to load the specified metadata resource”. Uh, the connection string information is right in the config file! The problem often can be resolved simply by:
    1. open the EDMX file in the designer mode 
    2. go to the properties of the EDMX 
    3. set the Metadata Artifact Processing property to “Copy to Output” 
    4. build the project 
    5. 然后从输出目录将*.msl.*.ssdl,*.csdl文件拷贝到应用程序目录底下即可。呵呵(估计很多人这里出错!)
  • 相关阅读:
    Winform中TextBox控件开启自动提示补全功能
    使用jsonp进行跨域访问
    sqlite不存在记录则插入数据
    【编译CEF3】编译Chromium(CEF3)源代码增加对mp3/mp4等格式支持的编译手记 完成编译,增加mp3/mp4等格式支持(3) 2018-5-21
    【编译CEF3】编译Chromium(CEF3)源代码增加对mp3/mp4等格式支持的编译手记 编译过程中所遇到的问题(2) 2018-5-20
    【编译CEF3】编译Chromium(CEF3)源代码增加对mp3/mp4等格式支持的编译手记 搭建编译环境的过程中所遇到的问题(1) 2018-5-19
    Git同步本地到Github失败
    Python基础知识总结
    Git命令
    Git仓库创建和文件提交
  • 原文地址:https://www.cnblogs.com/hanyun/p/2512503.html
Copyright © 2011-2022 走看看