0

PLEASE NOTE: None of the answers in the link above (which I don't seem to be able to remove) helped me. As I explain below, I had already tried all that stuff

I have a web site, developed in VS2013 using ASP.NET MVC5/WebAPI2, which has several related projects such as a service layer, repository layer, etc. Down at the bottom of the stack is a class library that holds an EF model. I have separated the actual entities into another class library, to allow them to be reused without requiring the model library.

All of this has been working fine. The web site was running, and I could make calls to the WebAPI methods as well.

I just uploaded the latest version to the production server, and all is working fine there. Then came back to VS to carry on work, and when I try to run up the web site, I get an exception

Unable to load the specified metadata resource

Searching around, it seems that the cure for this is to modify the connection string to point to the actual assembly name instead of using the default . I have two problems with this, first is that none of the config files in the solution have been touched today (by me at least, and the file history form source control confirms this), so there's no reason why it should suddenly stop working after being deployed, and second, even if I add the assembly name, I get the same exception.

Anyone any ideas what I can do? I'm completely stuffed now. Can't do anything.

Edit: I tried again to specify the assembly in the connection string, and now get the exception Unable to resolve assembly. I have checked the assembly name in a decompiler, and I'm pretty sure got it right.

Edit again: I just pulled the version that I deployed from source control, and that gives the same exception, so I'm sure this is nothing to do with any files I've changed (or even that have been changed by VS). The version on the production server is still working, but the source code that drives that exact same version gives the exception. So, I'm certain that the answer is NOT to be found in the myriad other versions of this question, but is somewhere else.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
  • possible duplicate of [MetadataException: Unable to load the specified metadata resource](http://stackoverflow.com/questions/689355/metadataexception-unable-to-load-the-specified-metadata-resource) – Amirhossein Mehrvarzi Nov 03 '14 at 12:46
  • Thanks, but as I already pointed out, I've checked the connection string, and haven't touched either the model or entities projects for several days. That's why I posted the question, because none of the answers in that (or the millions of similar posts) helped. Any other ideas? I just don't know what else to try. – Avrohom Yisroel Nov 03 '14 at 12:52
  • Anyone? I still haven't found an answer anywhere. – Avrohom Yisroel Nov 04 '14 at 09:06
  • `ConnectionString` isn't for setting assembly refrences! It only describes database path, credentials and the type of manager. For more focus Put the stack trace too:) – Amirhossein Mehrvarzi Nov 05 '14 at 09:46
  • Actually, the connection string for EF does take the assemblies into account, as it includes the metadata details. The default connection string uses * for the assembly, which causes it to search all loaded assemblies. The common solution to this problem is to replace the * with the assembly name. However, my problem was slightly different, please the answer I'm about to post which explains what happened. – Avrohom Yisroel Nov 05 '14 at 12:42

1 Answers1

1

Found the problem, and am posting it here in the hope that it will help someone else, as I don't think this was clear in any of the other posts on this issue.

I have a layered solution, with the web project referencing a service layer, which references a repository layer which in turn references the model project. It seems to for EF to work, whichever layer actually causes the database to be accessed requires a reference to the model project. My service layer project, which was where ToList() was being called (thus enumerating the query, and causing the database to be hit) didn't have a reference to the model project, so was failing to load the assembly.

I didn't need to alter the metadata part of the connection string either, as once the service layer had a reference to the model project, it was able to find the resources by itself. Having said that, one thing I did learn from all of this is that you can speed up the creation of the model (slightly) by specifying the assembly containing the resources explicitly, as this saves the framework having to search through all loaded assemblies to find them. I'm not sure if this will make any noticeable difference, but it can't harm.

I still can't explain how this had been working up until now, and suddenly stopped, as I hadn't changed any references, nor the way I was doing the data access. Still, it seems to be working now, which is all that matters.

Hope this helps someone.

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106