ADO.NET Entity Model metadata exceptions when using and EDMX file in a ASP.NET web site
Posted: (EET/GMT+2)
I recently needed to migrate parts of a graphical WinForms application to the web, and in the progress I needed to integrate the code with an existing ASP.NET web site. Note that I'm saying "web site" instead of a "web application", the latter of which I more commonly use myself.
Since web applications and web sites are different, the regular tips do not fully apply to entity models in web sites. During this migration project, I ran to the following error message:
System.Data.MetadataException: Unable to load the specified metadata resource.
This error is most often related to erroneous connection strings, which in Entity Framework take a format similar to this (lines shortened):
metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl| res://*/Models.MyModel.msl;provider=System.Data.SqlClient; provider connection string="data source=myserver; initial catalog=mydatabase;integrated security=True;multipleactiveresultsets=True; App=EntityFramework"
To solve this issue, you need to make sure that the "res://*/" specifications are correct. However, what most blog posts about this error fail to mention is that the name after the "res://*/" specification must match the of folder location of the .edmx file in the project. Usually, this in turn maps one-to-one to the code namespace of your application in the said .edmx class (you can see the namespace for example from the MyModel.Designer.cs file).
This is the key to solve the issue in an ASP.NET web site. Since all C# code in a web site must reside by default in the App_Code folder, then the metadata locations must reflect this. For instance, if the model .edmx filename is directly inside the App_Code folder, then the correct setting is:
metadata=res://*/App_Code.Models.MyModel.csdl ...
Remember to make the change to all the three res: specifications.
Hope this helps!