Unable to load the specified metadata resource – Entity Framework

We got this issue when we were calling the EDMX file from our DataAccess project. We used entity framework database first approach.

Our EDMX file lied in another project and the DataAccess project was used to do various operations on the EDMX file. But we forgot to add the connection string to the corresponding EDMX file into our DataAccess Project. Hence, this very ambiguous message :

[code language=”csharp”]"Unable to load the specified metadata resource" [/code]

The reason is Res://*/ is a URI which points to resources in the CURRENT assembly. If the Edm is defined in a different assembly from the code which is using it, res://*/ is not going to work because the resource cannot be found. So we need to add the connection string to all the projects that it’s being used in.
So I decided to dig a bit more and found out some possible reasons for this exception in entity framework including ours:

  • The EDMX resides in a separate project. We have to maintain connection string in both the projects. (This is our problem)
  • The MetadataArtifactProcessing property of the model You may have been changed to Copy to Output Directory.
  • There might be a post-compile task to embed the EDMX in the assembly and it’s giving errors for some reason.
  • The connection string itself is wrong. Yes I know it’s a very small thing, but it happens a lot.

 

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type

This error occurred in our application with a particular case when we stored two procedures with the same name in two different databases. These databases were internally related. We used Entity Framework database first approach for our application.

As EF uses only class names to identify the type mapped in EDMX (namespaces are ignored) – it is a convention to allow mapping classes from different namespaces to a single model, the EDMX could not differentiate the two classes generated from the stored procedures. So it threw this particular error-

[code language=”xml”]
The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type
[/code]

So the solution is:

  • Don’t use classes with the same unqualified name.
  • Name your stored procedures differently even if they’re in separate databases if you’re planning to use them in a single BLL.