简体   繁体   English

如何在 WinForms 中处理“在应用程序配置文件中找不到名为‘RestaurentEntities’的连接字符串”

[英]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.我正在尝试一个关于 WinForms 的项目。 I made a Form name Dashboard and User Control name DashboardScreen .我做了一个表单名称Dashboard和用户控件名称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() Dashboard基本上会有很多用户控件,如DashboardScreenInventoryScreenSettingsScreen等。这些屏幕很小并且停靠在主父窗体Dashboard因此当用户单击按钮时,相应的用户控件会使用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.这些用户控件正在实现Entity Framework V 6.4.0用户控件,当我不在这些控件上使用 Entity Framework 时,主仪表板表单工作正常。 Whenever I use Entity Framework as RestaurentEntities DB = new RestaurentEntities();每当我使用实体框架作为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.在应用程序配置文件中找不到名为“RestaurentEntities”的连接字符串。

Second Error is第二个错误是

The variable 'dashboardScreen' is either undeclared or was never assigned.变量 'dashboardScreen' 要么未声明,要么从未分配过。

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:如果要在表单中拖放 UserControl 并且构造函数方法或 OnLoad 方法中的控件可以访问 EntityFramework 并且 EntityFramework 访问是由引用的项目创建的,则必须在代码中包含以下内容:

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

Change your connectionStrings to below:将您的 connectionStrings 更改为以下内容:

<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你的 Dbcontext 构造函数就像

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


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在应用程序配置文件中找不到已命名的连接字符串 - No connection string named could be found in the application config file 在应用程序配置文件中找不到名为“CarAccountingEntities”的连接字符串 - No connection string named 'CarAccountingEntities' could be found in the application config file 在应用程序配置文件中找不到名为“ KgSoftProContext”的连接字符串 - No connection string named 'KgSoftProContext' could be found in the application config file InvalidOperationException:在应用程序配置文件中找不到名为“PayMyRentEntities”的连接字符串 - InvalidOperationException: No connection string named 'PayMyRentEntities' could be found in the application config file 在应用程序配置文件中找不到名为“ CREntities”的连接字符串 - No connection string named 'CREntities' could be found in the application config file 在应用程序配置文件中找不到名为“thenameofmyconnection”的连接字符串 - No connection string named “thenameofmyconnection” could be found in the application config file 在应用程序配置文件中找不到名为“MyEntities”的连接字符串 - No connection string named 'MyEntities' could be found in the application config file '在应用程序配置文件中找不到名为'MyEntities'的连接字符串。' - 'No connection string named 'MyEntities' could be found in the application config file.' EF6-在应用程序配置文件中找不到名为“ Data Source =…”的连接字符串 - EF6 - No connection string named 'Data Source=…' could be found in the application config file 发生了错误。 : 在应用程序配置文件中找不到名为“EmployeeDBEntities”的连接字符串 - An error has occurred. : No connection string named "EmployeeDBEntities' could be found in the application config file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM