When utilizing Entity Framework Entity Connection, a MetadataException occurs.



This key:

using (EntityConnection conn = new EntityConnection("name=ELSCommonEntities"))

the following error message:

Test method ELS.Service.Business.IntegrationTest.Base.ServiceBaseIntegrationTest.StartLoggingTestMethod threw exception:  System.Data.MetadataException: Unable to load the specified metadata resource..

Using the subsequent stack trace:

System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
System.Data.EntityClient.EntityConnection.SplitPaths(String paths)
System.Data.EntityClient.EntityConnection.GetMetadataWorkspace(Boolean initializeAllCollections)
System.Data.EntityClient.EntityConnection.InitializeMetadata(DbConnection newConnection, DbConnection originalConnection, Boolean closeOriginalConnectionOnFailure)
ELS.Service.Business.Base.ServiceBase.StartLogging(String userWindowsLogon) in C:\C-TOM\ELS-RELEASE1\ELS.Service.Business\Base\ServiceBase.cs: line 98
ELS.Service.Business.IntegrationTest.Base.ServiceBaseIntegrationTest.StartLoggingTestMethod() in C:\C-TOM\ELS-RELEASE1\ELS.Service.Business.IntegrationTest\Base\ServiceBaseIntegrationTest.cs: line 65

Although it makes use of the same connection string:

using (ELSCommonEntities db = new ELSCommonEntities())
    var res = from c in db.Logging
              select c;

    int i = res.Count();

does not indicate a mistake.

This is the connection string:

<add name="ELSCommonEntities" connectionString="metadata=res://*/Common.CommonModel.csdl|res://*/Common.CommonModel.ssdl|res://*/Common.CommonModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=els5_demo;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

Additionally, I opened the dll in reflector, and the information seemed to be in order.

2/2/2017 12:46:36 AM

Accepted Answer

Identified the issue.

The typical metadata string appears as follows:


And it usually works out perfectly. However, in certain cases (like mine), Entity Framework becomes perplexed and is unsure of which dll to search for. Change the metadata string as to:


And it will function. I was put on the correct path by clicking on this link:


Despite having the opposite issue, I worked in service rather than unit testing.

2/10/2013 9:11:52 PM

Popular Answer

However, I had to go a little further to find the solution and I wanted to share this little tidbit: I had the identical error message, and the issue was also the metadata portion of the connection string.

The metadata string is divided into three components, each of which is as follows:

      (model name).(ext)

In this case, "csdl," "ssdl," and "msl" are "ext."

The most common value for assembly is probably "*," which seems to mean that all loaded assemblies will be checked (I haven't tested this extensively). Although I have heard both proposed, this portion wasn't a problem for me, so I can't comment on whether you need the assembly name or file name (i.e., with or without ".dll").

In relation to your assembly, the model number section should be the Namespace and name of your.edmx file. The entire name of an assembly created from a My.DataAccess assembly in a Models folder is thus My.DataAccess.Models.DataModels. In this situation, your metadata would have "Models.DataModels.(ext)".

In my experience, you will need to manually update your metadata string if you ever relocate or rename your.edmx file; keeping in mind to modify the relative namespace can help you avoid some hassles.

Related Questions


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