J. Steen
J. Steen

Reputation: 15578

MetadataException: Unable to load the specified metadata resource

All of a sudden I keep getting a MetadataException on instantiating my generated ObjectContext class. The connection string in App.Config looks correct - hasn't changed since last it worked - and I've tried regenerating a new model (edmx-file) from the underlying database with no change.

Anyone have any ideas?

Further details: I haven't changed any properties, I haven't changed the name of any output assemblies, I haven't tried to embed the EDMX in the assembly. I've merely waited 10 hours from leaving work until I got back. And then it wasn't working anymore.

I've tried recreating the EDMX. I've tried recreating the project. I've even tried recreating the database, from scratch. No luck, whatsoever.

Upvotes: 734

Views: 565010

Answers (30)

Basheer AL-MOMANI
Basheer AL-MOMANI

Reputation: 15357

I spent a whole day on this error.

If you are working with n-tier architecture, or you tried to separate Models generated by EDMX form DataAccessLayer to DomainModelLayer, then you will get this error.

  1. The first troubleshooting step is to make sure the Connection strings in webconfig (UILayer) and appconfig (DataAccessLayer) are the same

  2. The second, which is very important, is the connection string .

    connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provid.....
    

The first part of the connection string is the problem. To see the place where the file names for the .csdl, .ssl, and .msl files come from, look in the solution folder as shown in the picture:

enter image description here

I hope this helps you!

Upvotes: 8

codedude
codedude

Reputation: 6549

I also received this error upon upgrading my project to .NET 6, and none of the previous solutions solved my issue.

You may want to check if your .edmx file has the following two properties set as follows:

  • Build Action: EntityDeploy
  • Copy to Output Directory: Copy Always

Ultimately my issue was that the files referenced in my .edmx file were not being generated.

Upvotes: 0

Spongebob Comrade
Spongebob Comrade

Reputation: 1568

Also in my case syntax error in the XML structure of the EDMX was the reason. I recognized that after VS was not able to show the graph diagram and opened the XML editor instead.

An earlier merge was the root cause of the syntax error.

Upvotes: 0

MicTech
MicTech

Reputation: 45113

A minor amendment helped me with this problem.

I had a solution with 3 project references:

connectionString="metadata=res://*/Model.Project.csdl|res://*/Model.Project.ssdl|res://*/Model.Project.msl;

which I changed to:

connectionString="metadata=res://*/;

Upvotes: 393

thehill
thehill

Reputation: 93

i deleted the \bin and \obj folders from all of the projects in the solution, then rebuilt the soluition and it worked fine

Upvotes: -1

Waleed.alhasan
Waleed.alhasan

Reputation: 137

I was reflecting an old program, I faced the same problem. I went through previous answers, and I succeeded to solve it by putting the 3 files of "Model.csdl", "Model.ssdl" and "Model.msl" in the bin directory, and also beside the entities class. After that change the metdata part of the entities connection string in web.config to be:

metadata=~/bin/Model.csdl|~/bin/Model.ssdl|~/bin/Model.msl

and the program ran successfully withput displaying this exception.

Upvotes: 0

Amol Aher
Amol Aher

Reputation: 189

I had same issue on 27 Sep 2019.

My API is in Dot net core Targeting to .net framework. edmx is in a different class library which is in .net framework only.

I observed that when I try to call that edmx from API .. for some reason I got that error.

What I did is, go to the obj folder of API and delete everything. then clean the API project and try again and that worked for me.

Upvotes: 0

MOH3N
MOH3N

Reputation: 1065

Sometimes i see this error in my project. I solve that by

1 - Right click on EDMX file

2 - Select Run Custom Tool option

3 - Rebuild project

Upvotes: 12

Liakat Hossain
Liakat Hossain

Reputation: 1384

This happens to me when I do not clean solution before build new .edmx designer. So just don’t forget to clean solution before you build new .edmx designer. This helps me to skip lot more issues with this one. Bellow the navigation details provided incase you are new in visual studio.

Click->Build->Clean Solution

Then Click->Build->Rebuild Solution

Hope this helps. Thanks everyone

Upvotes: 18

radkan
radkan

Reputation: 649

In my case none of the answers listed worked and so I'm posting this.

For my case, building on Visual studio and running it with IIS express worked fine. But when I was deploying using Nant scripts as a stand-alone website was giving errors. I tried all the suggestions above and then realized the DLL that was generated by the nant script was much smaller than the one generated by VS. And then I realized that Nant was unable to find the .csdl, .msl and .ssdl files. So then there are really two ways to solve this issue, one is to copy the needed files after visual studio generates them and include these files in the build deployment. And then in Web.config, specify path as:

"metadata=~/bin/MyDbContext.csdl|~/bin/MyDbContext.ssdl|~/bin/MyDbContext.msl;provider=System.Data.SqlClient;...."

This is assuming you have manually copied the files into bin directory of the website which you are running. If it's in a different directory, then modify path accordingly. Second method is to execute EdmGen.exe in Nant script and generate the files and then include them as resources like done in the example below: https://github.com/qwer/budget/blob/master/nant.build

Upvotes: 1

Graham Laight
Graham Laight

Reputation: 4840

If you are using the edmx from a different project, then in the connection string, change...

metadata=res://*/Data.DataModel.csdl

...to...

metadata=res://*/DataModel.csdl

Upvotes: 10

Craig Stuntz
Craig Stuntz

Reputation: 126587

This means that the application is unable to load the EDMX. There are several things which can cause this.

  • You might have changed the MetadataArtifactProcessing property of the model to Copy to Output Directory.
  • The connection string could be wrong. I know you say you haven't changed it, but if you have changed other things (say, the name of an assembly), it could still be wrong.
  • You might be using a post-compile task to embed the EDMX in the assembly, which is no longer working for some reason.

In short, there is not really enough detail in your question to give an accurate answer, but hopefully these ideas should get you on the right track.

Update: I've written a blog post with more complete steps for troubleshooting.

Upvotes: 887

RAM
RAM

Reputation: 485

I got this problem after moving a large solution from one folder in Source Control Explorer to another. We don't check the package folder into Team Foundation and so I think VS downloaded packages automatically. This upgraded my EF form v6.1.2 to v6.1.3.

The problem went away when I downgraded to the original v6.1.2.

Upvotes: 0

Christopher Townsend
Christopher Townsend

Reputation: 1745

In my case this was because I was building the connection string using a EntityConnectionStringBuilder. Make sure your Metadata property is using the Model name (including the namespace)

Upvotes: 0

Krishna shidnekoppa
Krishna shidnekoppa

Reputation: 1059

Exception is because compiler pointing to non existing Metadata so just Copy app.config connectionstring to Web.config ConnectionString

Upvotes: 3

Rick Arthur
Rick Arthur

Reputation: 2430

I had a similar error. I had recreated the project (long story), and pulled everything over from the old project. I hadn't realized that my model had been in a directory called 'Model' before, and was now in a directory called 'Models'. Once I changed the connection in my Web.Config from this:

<add name="RecipeManagerEntities" connectionString="metadata=res://*/Model.Recipe.csdl 

to this:

<add name="RecipeManagerEntities" connectionString="metadata=res://*/Models.Recipe.csdl

Everything worked (changed Model to Models). Note that I had to change this three places in this string.

Upvotes: 72

Pavel Nazarov
Pavel Nazarov

Reputation: 733

Just type path as follows instead of {Path.To.The.}: res:///{Path.To.The.}YourEdmxFileName.csdl|res:///{Path.To.The.}YourEdmxFileName.ssdl|res://*/{Path.To.The.}YourEdmxFileName.msl

Upvotes: 2

f.cipriani
f.cipriani

Reputation: 3527

I got this error when my emdx file was deleted by a prebuild command, quite simply. Took me a while before realizing it was that simple.

Upvotes: 0

ben
ben

Reputation: 6180

After hours of googling and trying to solve none of the solutions suggested worked. I have listed several solution here. I have also noted the one that worked for me. (I was using EF version 6.1.1, and SQL server 2014 - but an older DB)

  1. Rebuilding the project and try again.
  2. Close and open VS - I don't know how this works
  3. make sure if you have placed the .EDMX file inside a Directory, make sure you include the Directories in your ConnectionString. for example mine is inside DAL folder. SO it looks like this: connectionString="metadata=res://*/DAL.nameModel.csdl|res://*/DAL.nameModel.ssdl|res://*/DAL.nameModel.msl;(these are files. to see them you can toggle Show All Files in solution explorer, under ~/obj/.. directory)

...and many more which I had tried [like: reverting the EntityFramework version to a later version(not sure about it)]


what worked for me:

from this article here, it helped me solve my problem. I just changed my ProviderManifestToken="2012" to ProviderManifestToken="2008" in the EDMX file. To do this:

Solution Explorer

  1. Right click over file .edmx
  2. Open with..
  3. Editor XML
  4. Change ProviderManifestToken="XXXX" with 2008

I hope that helps.

Upvotes: 5

user464507
user464507

Reputation: 61

For my case, it is solved by changing the properties of edmx file.

  1. Open the edmx file
  2. Right click on any place of the EDMX designer
  3. choose properties
  4. update Property called "Metadata Artifact Processing" to "Embed in Output Assembly"

this solved the problem for me. The problem is, when the container try to find the meta data, it cant find it. so simply make it in the same assembly. this solution will not work if you have your edmx files in another assembly

Upvotes: 6

Pitming
Pitming

Reputation: 552

I had the same problem. I looked into my complied dll with reflector and have seen that the name of the resource was not right. I renamed and it looks fine now.

Upvotes: 6

JWP
JWP

Reputation: 6963

My issue and solution, the symptoms were the same "Unable to load the specified metadata resource" but the root cause was different. I had 2 projects in solution one was the EntityModel and the other the solution. I actually deleted and recreated the EDMX file in the EntityModel.

The solution was that I had to go back to the Web Application project and add this line into the config file. The new model had changed a few items which had to be duplicated in the "other" project's Web.Config file. The old configuration was no longer good.

     <add name="MyEntities"
     connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
                    provider=System.Data.SqlClient;
                    provider connection string=&quot;
                    data source=Q\DEV15;initial catalog=whatever;
                    user id=myuserid;password=mypassword;
                    multipleactiveresultsets=True;
                    application name=EntityFramework&quot;"
     providerName="System.Data.EntityClient" />

Upvotes: 4

Jared Beach
Jared Beach

Reputation: 3153

Using the info from this blogpost:

Like others have said, res:\\ is a pointer to your resources. To check and make sure your resource names are correct you can use a decompiler like DotPeek by JetBrains to open up your .dll file and see your Resources files.

Or you could open up the watch window while you're debugging and paste in this code to get an array of the resource names in the currently executing assembly.

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()

That being said, the format of your metadata paths should be something like:

{my-assembly-name}/{possibly-a-namespace}.{class-name}.{csdl or ssdl or msl}

Upvotes: 1

PeterX
PeterX

Reputation: 2901

I simply hadn't referenced my class library that contained the EDMX file.

Upvotes: 1

MyDaftQuestions
MyDaftQuestions

Reputation: 4701

A poor app.config or web.config file can do this.. I had copied the app.config connection string to my web.config in my UI and ended up entering:

<connectionStrings>
    <connectionStrings>
          <add name="name" connectionString="normalDetails"/>
    </connectionStrings>
</connectionStrings>

Upvotes: 1

Daniel Hollinrake
Daniel Hollinrake

Reputation: 1778

I had this problem yesterday and was looking at my code in debug and the output from SQL Profiler.

What I couldn't understand, before I read and understood this post, was why EntityFramework was throwing this error as it was calling the DB. I was looking through hundreds of lines in SQL Profiler trying to work out what was wrong with the database model. I couldn't find anything like the call I was expecting, and to be honest I wasn't certain what I was looking for.

If you are in this position, check the connection string. My guess is that before EntityFramework creates its SQL it will check the model, specified in the metadata part of the connection string. In my case it was wrong. EntityFramework wasn't even making it as far as the DB.

Make sure the names are correct. Once I got that sorted out, I was then seeing calls in SQL Profiler where the ApplicationName was 'EntityFramework' with SQL calling the expected tables.

Upvotes: 1

Rob Sedgwick
Rob Sedgwick

Reputation: 4514

Similar problem for me. My class name was different to my file name. The connectionstring generated had the class name and not the file name in. Solution for me was just to rename my file to match the class name.

Upvotes: 0

leqid
leqid

Reputation: 931

And a quick way to check the model name without Reflector.... look for the directory

...obj/{config output}/edmxResourcesToEmbed

and check that the .csdl, .msl, and .ssdl resource files are there. If they are in a sub-directory, the name of the sub-directory must be prepended to the model name.

For example, my three resource files are in a sub-directory Data, so my connection string had to be

metadata=res://*/Data.MyModel.csdl|res://*/Data.MyModel.ssdl|res://*/Data.MyModel.msl;

(versus metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;).

Upvotes: 30

صفي
صفي

Reputation: 1068

With having same problem I re-created edmx from Database. Solves my problem.

Upvotes: 3

Jeff Dunlop
Jeff Dunlop

Reputation: 893

When I got the metadata issue sorted out, I had a follow-on problem in the form of an invokation exception unable to find a connection string for XXXEntities in app.config (where my goal was no dependency on app.config). Through sheer luck I found that referencing System.Data in my unit test project cleared this final hurdle. So to summarise:

  1. Use nuget to install Entity Framework to your unit test project.
  2. Ensure System.Data.Entity and System.Data are referenced.
  3. Sort your connection string as described very well here.
  4. Pass the connection string to your partial class constructor.

I now have my metadata in a class library which can update from a reference db, and I can point my application and unit tests to any db on any server at runtime.

Addendum: When I moved my edmx to a folder, I got the error again. After a bit of research, I found that you want your metadata string to look like: metadata=res://EPM.DAL/Models.EPM.csdl, where EPM.DAL is the name of the assembly and EPM.edmx is in the models folder.

Upvotes: 0

Related Questions