我正在嘗試構建一個涉及WCF服務的解決方案,該服務調用一個包含EntityFramework6模型的dll。當我嘗試單元測試服務然而,我收到一條消息: Additional information: No connection string named 'SyslogEntities' could be found in the application config file.
EntityFramework6使用WCF服務的配置boundrary
我的流被邏輯地佈置爲:
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使用應該是真正的孤立。
那麼我做錯了什麼?
編輯: 我得到它運作良好,但也許是一種奇怪的方式。看來我的app.config中的設置沒有在安裝EF和MySql依賴項時由NuGet正確指定。
相反,我創建了一個代碼中配置類作爲這裏MSDN描述: https://msdn.microsoft.com/en-us/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());
}
}
我可以離開DB實現的細節到數據層,並且只在上層配置連接字符串。
謝謝!我正在調試一個解決方案中的所有東西(IIS 8 Express for debug),所以現在回想起來,單元測試可能會跳過服務託管,然後直接按照您的建議直接訪問dll。我通過添加連接字符串(我嘗試過在發佈之前無濟於事),而且還將實體框架提供程序,提供程序工廠和連接工廠定義添加到單元測試app.config中,從而得到它的工作。我有點驚訝,因爲它包含Mysql庫的引用,但它並沒有讓我直接引用dll。 –
奇怪的是,它看起來像一個基於代碼的定義效果更好。在將配置移動到單元測試類之前,我創建了一個配置類,並且一切正常,但是當我刪除配置類時,我開始出現提供程序定義錯誤。我認爲在通過NuGet安裝EF和MySql時,配置代碼不正確。謝謝你的幫助。 –