Entity Framework: Unable to load the specified metadata resource.

Recently, I was refactoring some code and I moved the location of the .edmx file into a different folder (and new namespace). I updated the code to use the new namespace but when I ran the application a MetadataException was thrown with the message “Unable to load the specified metadata resource.”

When the location of the edmx file changes, so does the connection string. The reason is that an Entity Framework connection string does more than a normal database connection string. So, if you move the edmx file, you also have to update the connection string so that the entity framework can continue to find the resources that the edmx file defines.

The connection string contains details of how the database is mapped to the entities by referencing the CSDL (Conceptual Schema Definition Language), SSDL (Store Schema Definition Language) and MSL (Mapping Specification Language) resources which are defined in the .edmx file, so if the location of the mapping changes then the connection string also needs to be updated so that the entity framework can continue to map the database to the entities.

For example, if you have a little application with two projects, an application project (in this case, the imaginatively names ConsoleApplication2) and class library (named DataAccess). An app.config file will be created for the data access project by the entity framework tools in Visual Studio. Normally that can be copied (or just the connection string entries at least) to the app.config (or web.config) of the main application.

At this point the connection string looks like this:

metadata=res://*/Products.csdl|res://*/Products.ssdl|
res://*/Products.msl;provider=System.Data.SqlClient;
provider connection string="data source=(local);
initial catalog=AdventureWorks;integrated security=True;multipleactiveresultsets=True;
App=EntityFramework"

As you can see it makes reference to the Products metadata in the calling assembly (that’s what the * means) which is split into the three resources (CSDL, SSDL & MSL) .

If a ModelEnties folder is created in the DataAccess project and the Products.edmx is moved into the ModelEntities folder then the location of the resource is moved, so the connection string is no longer valid. So, for the change that was just made the connection string needs to be updated to look like this:

metadata=res://*/EntityModel.Products.csdl|
res://*/EntityModel.Products.ssdl|
res://*/EntityModel.Products.msl;
provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=
AdventureWorks;integrated security=True;
multipleactiveresultsets=True;App=EntityFramework"

I’ve bolded the bits that have changed.

If you want to quickly get an updated connection string, you can open the edmx file and click in the design area then press F4 (or the menu View→Properties Window). The window will show Connection String property which can be copied and pasted into the config file.

About Colin Angus Mackay
I blog at ColinMackay.co.uk. I help run Scottish Developers which is a user group for software developers in Scotland, and co-organise the DDD Scotland conferences.

5 Responses to Entity Framework: Unable to load the specified metadata resource.

  1. vitor rubio says:

    You solved my problem! Thank you!

  2. David Shaddock says:

    That was fantastic. Craig Stuntz has a blog entry( http://blogs.teamb.com/craigstuntz/2010/08/13/38628/ ) that uncovered the problem but his link to your blog explained to me just what I was doing wrong in terms of forming the references. Thanks so much!!!

  3. Jeff Foster says:

    Well Written, thanks!!

  4. Demetrius Middleton says:

    Thanks ALOT!

  5. Pingback: 2012 in review « Blog of Colin Angus Mackay

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 26 other followers

%d bloggers like this: