10/3/12

Unable to load the specified metadata resource

Exception Description:


Exception Details: System.Data.MetadataException: Unable to load the specified metadata resource.

Resolution:


This error is often generated when we are using the Entity Framework for database access. The error means that there is a problem with finding the metadata information to allow the Entity Framework to work properly and translate queries against the conceptual model. At design time, the database entity model is created in the .edmx file. After compilation, the metadata information is stored as a resource in three different files:
  • .csdl  Conceptual Schema Definition Language
  • .ssdl  Store Schema Definition Language
  • .msl Mapping Specification Language
By default, these files get generated and embedded as a resource on the assembly. To find the location of these files, the Entity Framework uses the entities connection string in the config file. If you take a look at a config file, we will find this setting:

<connectionStrings>
    <add name="GalleryEntities" connectionString="metadata=res://*/Gallery.csdl|
res://*/Gallery.ssdl|
res://*/Gallery.msl;
provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\sqlexpress;initial catalog=…;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

For this project, we can see that the connection string indicates that all the files should be found on the default assembly’s (*) resources, and their names match the Gallery.* pattern. We are getting an error, so there is something not matching here. We will need to compile the code and inspect the assembly using reflector or ilspy.  These applications let us decompile assemblies, so we can browse their content. After opening the assembly, look at the resources folder, we should see the files there. In my case, there was a problem with the way the files get generated. The actual name of the files is Models.Gallery.*.  If you correct those entries in the config file, the connection string should now read as follows:

<connectionStrings>
    <add name="GalleryEntities" connectionString="metadata=res://*/Models.Gallery.csdl|
res://*/Models.Gallery.ssdl|
res://*/Gallery.msl;
provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\sqlexpress;initial catalog=…;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

We can now run the application, and the data should load with no problems.  If this was not the problem for your project, you should also make sure in which assembly the resource files are located. If it is not in the default assembly, you should replace the * in the connection string with the name of the correct assembly. For example:

connectionString="metadata=res://myassembly.dll/Models.Gallery.csdl|
res://myassembly.dll /Models.Gallery.ssdl|
res://myassembly.dll /Gallery.msl;

That indicates that those files are embedded as a resource in a different dll. I hope this provides some help in resolving this metadata exception.