我有下面的類:與構造溫莎城堡寄存器類參數
public class DatabaseFactory<C> : Disposable, IDatabaseFactory<C> where C : DbContext, BaseContext, new()
{
private C dataContext;
private string connectionString;
public DatabaseFactory(string connectionString)
{
this.connectionString = connectionString;
}
public C Get()
{
return dataContext ?? (dataContext = Activator.CreateInstance(typeof(C), new object[] {connectionString}) as C);
}
protected override void DisposeCore()
{
if (dataContext != null)
dataContext.Dispose();
}
}
當我嘗試啓動Web API,我得到以下錯誤:
Can't create component 'MyApp.DAL.Implementations.DatabaseFactory'1' as it has dependencies to be satisfied. 'MyApp.DAL.Implementations.DatabaseFactory'1' is waiting for the following dependencies: - Parameter 'connectionString' which was not provided. Did you forget to set the dependency?
我如何註冊正確,我如何在運行時傳遞參數?
的connectionString是變量。它如何傳遞給dependencyinstaller類? –
@ Ivan-MarkDebono你不能從配置文件中讀取它嗎?你需要有多個連接字符串嗎? –
連接字符串從單獨的數據庫中檢索,並且用戶在登錄時選擇其中一個。EF模型總是相同,但用戶可以選擇多個不同的數據庫。 –