MetadataException when using Entity Framework Entity Connection

This code:

using (EntityConnection conn = new EntityConnection("name=ELSCommonEntities")) { conn.Open(); } 

Gives me the following error:

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

With the following 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) System.Data.EntityClient.EntityConnection.Open() 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 

However, this code uses the same connection string:

 using (ELSCommonEntities db = new ELSCommonEntities()) { var res = from c in db.Logging select c; int i = res.Count(); } 

It does not give an error.

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" /> 

I also opened the DLL in the reflector and the metadata looks fine.

+61
entity-framework
Jul 02 '09 at 13:28
source share
8 answers

Found a problem.

The standard metadata string is as follows:

 metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl 

And it works great in most cases. However, in some (including mine) Entity Framework get confused and do not know which DLL to look for. Therefore, change the metadata line to:

 metadata=res://nameOfDll/Model.csdl|res://nameOfDll/Model.ssdl|res://nameOfDll/Model.msl 

And it will work. It was this link that led me to the right path:

http://itstu.blogspot.com/2008/07/to-load-specified-metadata-resource.html

Although I had a problem with oposite, it did not work in unit test, but it worked in the service.

+105
Jul 03 '09 at 14:26
source share

I had the same error message, and the problem was also part of the connection string metadata, but I had to dig a little deeper to solve it and wanted to share this little screw:

The metadata line consists of three sections, each of which looks like this:

 res:// (assembly)/ (model name).(ext) 

Where ext are "csdl", "ssdl" and "msl".

For most people, the assembly may probably be β€œ*”, which seems to indicate that all loaded assemblies will be loaded (I have not done huge tests). This part was not a problem for me, so I can’t comment on whether you need the assembly name or the file name (ie With or without ".dll"), although I saw both sentences.

Part of the model name should contain the name and namespace of your .edmx file relative to your assembly. Therefore, if you have the My.DataAccess assembly and you create DataModels.edmx in the Models folder, its full name is My.DataAccess.Models.DataModels. In this case, you have "Models.DataModels. (Ext)" in your metadata.

If you ever move or rename your .edmx file, you will need to manually update the metadata line (in my experience), and remember that a few headaches remain to change the relative namespace.

+42
Oct 12
source share

There are several possible catches. I think the most common error in this part of the connection string is:

Res: //xxx/yyy.csdl | Res: //xxx/yyy.ssdl | resolution: //xxx/yyy.msl;

This is not magic. Once you understand what it means, you get the connection string correctly.

The first part is xxx. This is nothing more than the name of the assembly where you defined the EF context contexts. Usually it will be something like MyProject.Data. The default value is *, which indicates all loaded assemblies. It is always best to specify a specific assembly name.

Now part of yyy. This is the name of the resource in assembly xxx. Usually this will be the relative path to your .edmx file with periods instead of a slash. For example. Models / Catalog - Models.Catalog The easiest way to get the right line for your application is to build the xxx assembly. Then open the assembly of the dll file in a text editor (I prefer the default Total Commander viewer) and search for ".csdl". Usually this line should not be more than 1.

Your last line of EF connection might look like this:

Res: //MyProject.Data/Models.Catalog.DataContext.csdl | resolution: //MyProject.Data/Models.Catalog.DataContext.ssdl | Res: //MyProject.Data/Models.Catalog.DataContext.msl;

+14
Mar 18 '13 at 21:13
source share

As Shiraz Bhaide replied, this is metadata = res: ///Model.csdl | res: ///Model.ssdl | res: //*/Model.msl. However, I still had problems building the correct string based on my model localization, namespaces, and assembly name. A very simple solution was to rename the .edmx file in Visual Studio (after renaming and return to the original name), which caused the line to be automatically updated in my Web.config

0
Aug 07 2018-12-12T00:
source share

I had the same problem with three projects in one solution, and all the suggestions didn't work until I made a link in the link file of the website project to the project where the edmx file is located.

0
Jul 17 '13 at 11:55
source share

I moved the first DataModel database to another project halfway through development. Poor planning (or lack thereof) on my part.

Initially, I had a solution with one project. Then I added another project to the solution and recreated the first DataModel from Sql Server Dataase.

To fix the problem - MetadataException when using Entity Framework Entity Connection . I copied my ConnectionString from the new Project Web.Config to the original Web.Config project. However, this happened after I updated all the links in the original project to the new DataModel project.

0
Jan 20 '14 at 18:31
source share

It may just be a connection string error, which is solved by the above process, but if you use the DLL in several projects, then make sure that the connection string is named correctly, it will certainly fix the error.

0
Feb 15 '15 at 17:51
source share

I had this problem when moving my first .edmx database .edmx from one project to another.

I just did the following:

  • Removed connection strings in app.config or web.config
  • Deleted 'Model.edmx'
  • Re-added the model to the project.
0
Nov 22 '16 at 16:34
source share



All Articles