views:

189

answers:

2

i have a one entity framework object and when i add it to my project, the connectionstring is add to app.config in connectionstring section, but when i want create new entitycontext and use this connectionstring, this error is appear

A: 

I suspect that your issue is coming from the fact that you have more than one project in your solution and the one that contains your entity framework stuff including edmx files is NOT the solutions's startup project. In this case even if the connection string exists in the EF app.config project, still CLR cannot find it at runtime. For example if you have a web site and a EF project in your solution, you need to copy the connection string from the EF project's app.config to your website's web.config. Basically any connection string data should be exist in the config file of the project that the .Net threads initiated from by CLR (i.e. your startup project). If this is not your case, then just open your edmx file, right click on its surface, select properties and copy the connection string and paste it into your app.config Connection String section. This way you can make sure that you are having the correct one in your config.

EDIT:
As you can see here on Documenation on ObjectContext Constructor, the first parameter is the connectionstring name which is code generated at the time you create your EDM. If, somehow, the name of your connectionstring name happens to be changed, all you need to do is right clicking on your model and selecting "Update Model From Database..." then follow the wizard to update your confing and designer to reflect this change.

Morteza Manavi
hi Morteza and thank for your answer, but i previously copy the connectionstring sectin in web.config, but the error is not solved,but when in entitymodel.designer replace(public EntityContext(): base("name=EntityContext", EntityContext"))whit the connectionstring this worked,any idae
A: 

I ran into this problem when I tried to put my custom database logic in a .dll to be used by multiple projects in my solution.

While the .dll had the correct app.config file, it didn't work. Entity frameworks wanted the connection information in the app.config of the .exe. Copying the information to there worked just fine.

Morteza's solution of pasting the connection string directly into the .edmx didn't work for me, as it wouldn't let me paste the value in there -- although that's precisely what I wanted to be able to do.

Walt Stoneburner