Sunday, July 26, 2015

C# MetadataException: Unable to load the specified metadata resource error

This post will be about how to fix the "MetadataException: Unable to load the specified metadata resource" error in your ASP.NET web MVC and other applications. I'll keep this one short - what is going on is that the connection string that you used to setup the edmx for the entity model is different from the one now in your Web.Config file. How does it happen and what to do about it? Please see the below points -

  • Check to see if you recently changed connection strings for production deployment. This is actually the most common reason behind this problem.
  • Check the entity names for csdl, ssdl and msl in the connection string in the Web.Config file. These appear right after the "metadata=res://*/" part of the connection string in the Web.Config file.
  • One thing to keep in mind (that also follows from the above points) is that the actual database name and connection infromation is most likely correct. I'm saying this because when we think of connection strings, we tend to check only the database name and connection information. In this case it's the stuff that comes before the actual connection info (that is, before the "provider..." part of the string) that is likely to be messed up.
  • Sometimes we copy paste the conenction strings between different applications. This is fine but remember that if you do it, then you must change the names of the csdl, ssdl and msl as well in addition to key and the actual connection info. Copying pasting connection strings between applications is another thing that usually causes this problem.

So bottomline, if you encounter the "MetadataException: Unable to load the specified metadata resource" error, then check each and every letter in your connection string and ensure that the parts at the beginning (the ones between "metadata=//*/" and "provider") match with what was used to create the edmx entity model initially.