Referring ADO.Net Entity project in a different project

I just now answered a question on the forum about one of the errors that might take you a while to figure out. Assume the following scenario, you have a Data Model Project designed as class library for EntityFramework and you have a middle layer or a UI Layer and you are referring the EF Assembly. When you try to run the project you get the  following error

"The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid."

If you go check the EntityProject you might find the connectionstring information inside the app.config. This is created automatically by the Entity Model Designer wizard. The issue is that when you refer the EntityProject inside another project the dll for the entity model is loaded in the context of the current application and hence it tries to read the connectionstring from the current app.config/web.config file.

The fix is to add the connectionstring information from the EntityProject  to your referring project’s app.config or web.config under the <connectionstrings> element , like this

   <add name="SchoolEntities" connectionString="metadata=res://*/SchoolModel.csdl|res://*/SchoolModel.ssdl|res://*/SchoolModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\yukon;Initial Catalog=School;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

Here is the forum post on this