2015-02-10 35 views
1

我正在創建一個自定義用戶控件,最終將用於多個項目。這將是一個胖控件,它包含許多業務邏輯並訪問SQL數據庫以加載數據並驗證用戶操作。 (我的一個要求是儘可能簡單地將它放到一個新窗體上。)所以我有一個「Windows窗體控件庫」項目,裏面有一個用戶控件。我用Entity Framework 6設置了一個edmx。我編寫了一個針對我的數據模型的LINQ查詢,它返回了幾行數據,並使用這些數據填充組合框。現在我想測試一下。因此,我將一個WinForms項目添加到解決方案中,將我的控件添加到Form1並運行測試項目。我得到以下錯誤:實體框架6和自定義控件

No connection string named 'MyDatabaseEntities' could be found in the application config file.

正確...我需要用所有EF6配置更新App.config。所以,我從我的控件庫項目中複製批量我App.Config中進入App.Config中爲我的WinForms測試項目和我結束了:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    </configSections> 
    <connectionStrings> 
    <add name="MyDatabaseEntities" connectionString="metadata=res://*/MyDatabase.csdl|res://*/MyDatabase.ssdl|res://*/MyDatabase.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MyServer;initial catalog=MyDatabase;persist security info=True;user id=testuser;password=testpassword;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="v11.0" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 

現在,當我運行測試項目中,我得到這個錯誤:

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

我可以通過在測試項目中添加EF6來解決這個問題,但這看起來不正確。我希望我的控件能夠封裝所有的數據訪問。只要我在App.Config中有EF配置信息,那應該就夠了,對吧?但是,如果我將EF添加到測試項目中,顯然有一些缺失會得到修復。任何人都可以告訴我還有什麼我需要做的測試項目,短添加EF? (還是有更好的辦法,完全可以做我正在拍攝的東西?)

謝謝!

+0

我在測試項目的\ bin \ debug \文件夾中看到4個EF程序集:Microsoft.Practices.EnterpriseLibrary.Caching.dll,Microsoft.Practices.EnterpriseLibrary.Common.dll,Microsoft.Practices.EnterpriseLibrary.Data.dll,Microsoft .Practices.EnterpriseLibrary.ExceptionHandling.dll – DeadZone 2015-02-19 13:08:45

+0

D'oh!對於那個很抱歉。一次在幾個地方得到了我的頭。我在我的\ bin \ debug \文件夾中有EntityFramework.dll。應該不止於此? – DeadZone 2015-02-19 14:23:22

+0

那你知道什麼?看起來這是解決方案。所以我想我需要確保在我的控制權下分發這兩個程序集(這些都是內部的,但是我們希望爲其他開發者提供某種分發包)。但似乎是這樣。謝謝。請將此作爲答案提交,以便我可以接受,並且您可以申領獎金。 – DeadZone 2015-02-19 15:12:19

回答

4

當你的web.config包括這一行:

<provider 
    invariantName="System.Data.SqlClient" 
    type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 

你需要在你的應用程序bin文件夾正確的DLL。在這種情況下,所需的庫是EntityFramework.SqlServer.dll。但是,您幾乎肯定也需要EntityFramework.dll