1
votes

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>
2
Most likely you will need to add the connectionStrings to the .config file of the project that is actually calling the new RestaurentEntities objectJawad

2 Answers

1
votes

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
        }
 }
0
votes

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")
    {


    }