[英]EntityFramework6 Configuration with WCF Service boundrary
我正在嘗試設計一個涉及WCF服務的解決方案,該服務將調用包含EntityFramework6模型的dll。 但是,當我嘗試對該服務進行單元測試時,收到一條消息: Additional information: No connection string named 'SyslogEntities' could be found in the application config file.
我的流程在邏輯上排列為:
SyslogDataSvcTest.dll (app.config has service bindings) ->
SyslogDataSvc.svc (web.config has provider and connection string) ->
LibSyslogData.dll (app.config has providers and connection string)
所有涉及EF的代碼都在libSyslogData.dll中。 它在內部將數據映射到上層對象,然后將其返回,而不是公開自己的模型信息,因此EF的使用應真正隔離。
那我在做什么錯?
編輯:我的工作很好,但也許很奇怪。 似乎在安裝EF和MySql依賴項時,NuGet未正確指定我的app.config中的設置。
取而代之的是,我創建了一個代碼內配置類,如MSDN所述: https : //msdn.microsoft.com/zh-cn/data/jj680699
現在我有這個課:
public class EFConfiguration : DbConfiguration {
public EFConfiguration()
{
//For some reason this works much better than the app.config.
//With this defined, upper layer config documents need only specify the "SyslogEntities" Connection string.
SetExecutionStrategy("MySql.Data.MySqlClient", () => new MySqlExecutionStrategy());
SetDefaultConnectionFactory(new MySqlConnectionFactory());
SetProviderFactory("MySql.Data.MySqlClient", new MySqlClientFactory());
SetProviderServices("MySql.Data.MySqlClient", new MySqlProviderServices());
}
}
我可以將數據庫實現的細節留給數據層,而僅在上層配置連接字符串。
您很可能不會基於該異常測試實際的wcf服務(我的意思是在線)。 我認為您只是對wcf服務dlls代碼進行單元測試。 因此,這意味着所使用的app.config來自單元測試程序集。 無論如何,只有1個用於應用程序運行時的app.config,具有用於類庫的app.config也是沒有意義的。
您需要提供EF數據訪問層在單元測試程序集中使用的EF連接字符串,就像在wcf服務web.config中所做的一樣(因為您沒有測試在線wcf服務)。 如果要在單元測試和wcf服務配置中完全擺脫EF引用,則需要覆蓋EF實體的默認構造函數,並在應用程序/ Web配置中提供“普通” dbprovider + connectionstring並創建EF連接飛行中的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.