Lesson: The EDMX file name must match the EF model's metadata part names
Corollary: If you rename your edmx file, remember to update your connection string.
My Sob Story:
I received the following error when my new app ran its very first query:
The model's metadata (csdl, msl, ssdl) could not be found or is invalid. This error can also occur if multiple assemblies containing the same metadata are found. Check the connection string for key 'NorthwindIBEntities'.
My application solution was simple ... a Silverlight project and a Web project. Only the web assembly could have mattered. Here's a picture of my Web Project in Solution Explorer:
I looked at the web.config. The connection string - 'NorthwindIBEntities' - was right there!
<add name="NorthwindIBEntities" connectionString="metadata=res://*/NorthwindIB.csdl|res://*/NorthwindIB.ssdl|res://*/NorthwindIB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
I opened the Entity Framework Designer and asked it to "Update Model From Database" ... knowing it would have to use this string to find the database. No problem. The string is a valid EF connection string.
I looked at the debug.log ... always one of the first steps to take ... and saw that the server was starting up fine and the client connecting to it just fine. It reported my query attempt and the error message I had received.
I inspected my EntityManager in the debugger: yes indeed ... I'm connected and logged in.
What could I be doing wrong?
The Answer Revealed
A keen-eyed colleague figured it out.
Notice that the name of the edmx file is "Model1.edmx". Notice that the EF connection string refers to "NorthwindIB" in the names of the CSDL, SSDL, MSL part files.
Apparently EF doesn't pay attention to this connection string. It always writes the metadata part files using the name of the edmx file.
DevForce can't know about this. DevForce can only look at the connection string and assume that it correctly identifies the metadata file parts that belong to that string.
Therefore, take heed: the part names must match the edmx file name. Given that my model is named "Model1.edmx", the connection string should have read:
<add name="NorthwindIBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=NorthwindIB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
I made this change in my web.config ... and soon was running fine.
How Did I Get Into This Mess?
I had cobbled together a new application, borrowing parts and pieces from an existing application.
This exercise consistently gets me into more trouble than starting from scratch ever does. As it happened, I had filtched a web.config from another app that reads from NorthwindIB. Unfortunately, I didn't steal the edmx at the same time; instead I created a new model in my new app ... with a different file name ... and thus my sad tale.
You have been warned :-)
Edited by WardBell - 21-May-2010 at 5:11pm