简体   繁体   中英

How to handle "No connection string named 'RestaurentEntities' could be found in the application config file" in WinForms

I hope you all will be fine.

I am trying a project on WinForms. I made a Form name Dashboard and User Control name DashboardScreen . Dashboard basically will have many User Controls on it like DashboardScreen , InventoryScreen , SettingsScreen etc. These screens are small and Dock in main parent form Dashboard so when user click on button, corresponding user control comes to the front using DashboardScreen.BringToFront()

These user controls are implementing Entity Framework V 6.4.0 User controls and Main Dashboard Form works fine when I do not use Entity Framework on these controls. Whenever I use Entity Framework as RestaurentEntities DB = new RestaurentEntities(); to utilize database services the Main Dashboard Form gives the following Error in Design Mode.

No connection string named 'RestaurentEntities' could be found in the application config file.

Second Error is

The variable 'dashboardScreen' is either undeclared or was never assigned.

Given is my configuration file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings><add name="RestaurentEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=YASIR\SQLDEVENV;initial catalog=Restaurent;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

if you want to drag and drop a UserControl in a form and the control in the constructor method or OnLoad method has access to EntityFramework and the EntityFramework access is created by referenced project you must include into your code something like:

 protected override void OnLoad(EventArgs e)
 {
        base.OnLoad(e);
        if (!DesignMode)
        {
            //access to EntityFramework
        }
 }

Change your connectionStrings to below:

<connectionStrings>
    <add name="RestaurentEntities" 
   connectionString="data source=YASIR\SQLDEVENV;initial catalog=Restaurent;integrated 
   security=True;MultipleActiveResultSets=True;App=EntityFramework" 
   providerName="System.Data.SqlClient" />
</connectionStrings>  

And make sure that

your Dbcontext constructor is like

    public RestaurentEntities ()
        : base("name=RestaurentEntities")
    {


    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM