Visual studio VS2015设计器错误:键';Main.ConnectionString';appSettings配置部分中不存在

Visual studio VS2015设计器错误:键';Main.ConnectionString';appSettings配置部分中不存在,visual-studio,visual-studio-2015,Visual Studio,Visual Studio 2015,尝试在设计器中打开某些窗体时出错 这是堆栈跟踪: at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value) at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkPropertyDescriptor.SetValue(Object component, Object value) at System.Com

尝试在设计器中打开某些窗体时出错

这是堆栈跟踪:

    at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkPropertyDescriptor.SetValue(Object component, Object value)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement) 
这是我的app.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="Main.ConnectionString" value="Server=localhost;Database=ACTUAL_DB_NAME_HERE;User ID=ACTUAL_USER;Password=ACTUAL_PASSWORD;"/>
  </appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>

当您试图在designer中打开表单时,您的表单无法访问app.config。这是因为您的程序没有运行,表单也不打算显示数据库中的相关数据。这是故意的。它只用于显示表单布局和视觉效果。因此,在设计模式下,您不能尝试打开数据库连接、读取文件、播放视频等

有关详细信息,请参阅

如果此表单上的组件是由您编写的,请尝试在组件
Load
事件中将中断设计器的代码包装为以下内容:

if ( this.Site == null || !this.Site.DesignMode )
{
... // code that breaks the designer
}

编辑:最佳做法是将连接字符串存储在app.config的专用部分。我认为这可能与表单控件中以某种方式序列化的数据有关。我在2010年处理数据库关系图时遇到过类似的问题。在大多数情况下(System.Configuration.ConfigurationManager.ConnectionStrings[“dbConnString”].ConnectionString),它可以很好地处理配置文件,但这取决于我运行代码的方式,会导致问题。通过在设置文件中添加字符串并使用
BaseClass.Properties.settings.Default.DBConnString
访问它,我获得了更全面的成功。这可能会帮助您吗?在我的例子中,字符串(我相信)是从我的ORM库访问的。我没有直接从代码中访问字符串,但我的表单上有ORM对象。@scotru您是如何将密钥添加到app.config的?手动?您的连接字符串位于AppSettings中。我认为它应该存储在ConnectionString元素下。签出并感谢--我能够将其追溯到一个数据感知用户控件,该控件试图在设计时加载数据。我使用了您描述的控制策略,并且能够解决问题。