Wednesday, June 22, 2011

Entity Framework, web.config and error "The specified named connection is either not found in the configuration..."

Hi my friend,

If  you come to here from generic search, then you already know what is a real nightmare. Every new feature from Microsoft can include surprised items and will take hours (sometimes days) for understand how it work correct. Anyway...

For using Entity Framework as DLL class-library in win/console application you should work by next steps:

1. Create YOUR_NAME.edmx.
2. Open designer.
3. Click on background.
4. In property tab change property "Metadata artifact Processing" to "Copy to Output Directory".
5. Save project (Connection string will be changed from "res//*/" to ".\" ).
6. Copy connection string to main app.config as is.
7. Go to EF project properties. Choose "Build event" and in Post-Build event command line add next rows:

copy  YOUR_NAME.csdl $(SolutionDir)START_PROJECT\bin\Debug\
copy  YOUR_NAME.ssdl $(SolutionDir)START_PROJECT\bin\Debug\
copy  YOUR_NAME.msl $(SolutionDir)START_PROJECT\bin\Debug\

8. Remove app.config of EF project.

But the nightmare does not live in win application, because she's place in Web Application!

Next steps will help you to back out from hell:
Do it 1-6 steps as in win application case.
7. In web.config change  ".\" to "~\App_Data" in c/m/s files
8. Go to EF project properties. Choose "Build event" and in Post-Build event command line add next rows:

copy  YOUR_NAME.csdl $(SolutionDir)START_WEB_PROJECT\App_Data\
copy  YOUR_NAME.ssdl $(SolutionDir)START_WEB_PROJECT\App_Data\
copy  YOUR_NAME.msl $(SolutionDir)START_WEB_PROJECT\App_Data\

9. Remove app.config of EF project.
10. Cross fingers, run it and pray!

Enjoy!

No comments: