Background:
When I was working on the software today, I encountered a problem. I added the Entity Framework to the project. There was no problem with the reference in the form code, and there was a problem with the reference in usercontrol.
I checked the app. config file
The file contains the connection string, but cannot be read.
Cause:
EF uses the app. config ofCurrent Application. That means that when you're designing the controls inside Visual Studio, it'll use devenv.exe. config. the connection isn' t listed there. also because DB access can have other side-effects (slow down the designer, cause unwanted dB queries), it's best to turn this off at design time.
EF will call the app. config file in the design mode, but the vs configuration file is called at this time, of course, there is no connection string we want.
Solution:
Do not call EF during design.
Code:
if (DesignMode){ return;}using (var edm = new StudentManageEntities()){ //do something here}
User Control references Entity Framework