Metadata Exception in Entity Framework Connection Strings

After generating the entity model and classes for my existing SqlCe database, I tried to run some LINQ queries against it.  The following exception was thrown as soon as I tried to instantiate the ObjectContext:

unable to load the specified metadata resource

The metadata that the error message refers to is in the connection string.  This was automatically generated for me, so why would it fail?  After lots of googling, I found this page on Craig Stuntz's blog that has some very handy troubleshooting tips.  He lists several possibilities as to why the metadata would be the cause of the error, but in my case it was the use of the asterisks in the paths in the embedded resources:

metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl

As Craig points out, it is better to replace the asterisks with the explicit name of the assembly that contains the model as in:

metadata=res://MyDAL/MyModel.csdl|res://MyDAL/MyModel.ssdl|res://MyDAL/MyModel.msl

If this doesn't work for you, you may need to use a tool such as Reflector to find the exact path of the resources within the compiled DLL.  In my case, my model was in a sub-directory of the DAL project (named "Client"), so my metadata now looks like this:

metadata=res://MyDAL/Client.MyModel.csdl|res://MyDAL/Client.MyModel.ssdl|res://MyDAL/Client.MyModel.msl

I haven't encountered the metadata exception since I made this change.
Ebook Download
View all
Learn
View all