2015-09-18 44 views
1

尝试在设计器中打开一些表单时发生错误。VS2015 Designer错误:appSettings配置部分中不存在键'Main.ConnectionString'

这是堆栈跟踪:

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> 
+0

我认为这可能与数据以某种方式序列化在窗体控件中。 – scotru

+1

我在2010年使用数据库图时遇到了类似的问题。它在大多数情况下都能很好地与配置文件一起工作('System.Configuration.ConfigurationManager.ConnectionStrings [“dbConnString”] .ConnectionString'),但取决于我如何运行代码,这会造成麻烦。通过在设置文件中添加字符串并使用'BaseClass.Properties.Settings.Default.DBConnString'访问它,我获得了更大的成功。这可能会帮助你吗? – Heki

+0

在我的情况下,字符串(我相信)正在从我的ORM库进行访问。我没有在任何地方直接从我的代码访问字符串 - 但我的表单上有ORM对象。 – scotru

回答

1

当你试图打开你的设计形式,你的app.config是不是你的形式访问。这是因为你的程序没有运行,并且表单不打算显示来自数据库的相关数据。这是设计。它旨在表现形式布局和视觉效果。因此,在设计模式下,您不得尝试打开数据库连接,读取文件,播放视频等。

查看this question了解详情。

如果该表格上的组件被写了你,尽量包裹,打破了设计师在组件Load事件在下面的代码:

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

编辑:最佳做法是存储连接字符串在app.config的特定部分。 Here is a good explanation这个。

+0

谢谢 - 我能够跟踪到一个数据感知用户控件,试图在设计时加载数据。我使用了您在控件上描述的策略,并能够解决问题。 – scotru