無法加載指定的元數據資源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文件拷貝到應用程序目錄底下即可。呵呵(估計很多人這里出錯!)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM