2015-09-18 45 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