我正嘗試使用一個變量訪問不同的上下文。請看看之前的代碼:c#如何製作一個動態的Context,來訪問不同的數據庫?
...
private readonly ClientOneType _contextClientOne;
private readonly ClientTwoType _contextClientTwo;
public ExampleService()
{
_contextClientOne = new ClientOneType();
_contextClientTwo = new ClientTwoType();
}
public Stores[] GetStores(Store storeModel)
{
try
{
var _dynamicContext = null; //this throws an error because c# needs a type for runtime.
if (client == "OutBack")
_dynamicContext = _contextClientOne;
else if(client == "DollarGeneral")
_dynamicContext = _contextClientTwo;
var stores = (from s in _dynamicContext.Store //this is where the magic should take place
where s.StoreName == storeModel.StoreName
select p).ToArray();
return stores;
}
...
}
運行,是因爲_dynamicContext不能爲空,所以我怎樣才能創建一個可改變成不同的上下文變量時我得到一個錯誤?
懶惰的解決方案是爲每個客戶端創建不同的方法,但這不會很有效,因爲它將變得無法維護。
我真的很感謝幫助。先謝謝你。
爲我工作很好,非常感謝你。 – RDevAwe