2010-06-19 45 views
0

我不斷收到此錯誤:WPF和ADO.NET EF - 錯誤

Microsoft.Practices.Composite.Modularity.ModuleInitializeException was unhandled 
    Message=An exception occurred while initializing module 'DiagrammerModule'. 
    - The exception message was: An exception has occurred while trying to add a view to region 'MainRegion'. 
    - The most likely causing exception was was: 'System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid. 
    at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) 
    at System.Data.EntityClient.EntityConnection..ctor(String connectionString) 
    at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString) 
    at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName) 
.... 

連接字符串存儲在app.config中,看起來像這樣:

<connectionStrings> 
    <add name="DBEntities" connectionString="metadata=res://DataAccessLayer/EntityDataModel.csdl|res://*/EntityDataModel.ssdl|res://*/EntityDataModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=&quot;Data Source=|DataDirectory|\DB.sdf;Password=password;Persist Security Info=True&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 

其中DataAccessLayer是名稱包含模型和實體的DLL的一部分。

該代碼有什麼問題?

感謝您的幫助。

回答

0

看起來像數據上下文構造函數在配置文件中找不到連接字符串。檢查文件被複制到應用程序輸出目錄,並且名稱爲{你的WPF應用程序名稱} .exe.config(不是{你的DLL包含數據上下文} .dll.config)。

+0

我也許應該補充一點,這是棱鏡應用。我正在按照這個解決方案(滾動到頁面的底部)插入一個dll的名稱:http://geekswithblogs.net/hoarked/archive/2009/02/04/creating-a-wpf-application -with棱鏡-V2-ndash的-瀏覽-module.aspx – Enzomatric 2010-06-19 17:10:57

0

從您提供的鏈接中可以看到: 將「*」替換爲包含該文件的DLL的名稱。

所以一定要更換的「*」如下所有實例:

<connectionStrings> 
    <add name="DBEntities" connectionString="metadata=res://DataAccessLayer/EntityDataModel.csdl|res://DataAccessLayer/EntityDataModel.ssdl|res://DataAccessLayer/EntityDataModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=&quot;Data Source=|DataDirectory|\DB.sdf;Password=password;Persist Security Info=True&quot;" providerName="System.Data.EntityClient" /> 
</connectionStrings>