如果我有一個dbContext誰的連接字符串是在上下文創建過程中創建的,然後我想根據一些信息來映射表名......如何將該數據傳遞給表映射?例如:動態DbContext映射表
private object createContext(string name, string country)
{
DbConnectionstringbuilder conn = new DbConnectionstringbuilder();
conn.Add("Provider", "System.Data.SqlClient");
conn.ConnectionString = string.Format(name, "dbName_" + country + "_moreName");
//maybe an if/else since we will say there are more names and countries
return new object(conn.ToString());
}
現在,當你初始化的DbContext是這樣的:
public class object : DbContext
{
public object(string conn):base(conn)
{
DbDatabase.SetInitializer<object>(null);
}
//insert dbset and mapping config calls here
}
現在我想我的表映射到喜歡 「table_」 +國+ 「_endtablename」;我猜如何將數據傳遞給映射調用?
這些表映射到使用此格式的現有數據庫。該國實際上來自登錄用戶的憑據,因此基於位置等。我只需要找到一種方法來訪問該國家代碼,該代碼是後者創建的dbcontext中的基類的內部構造函數。
馬克感謝您的迴應。我將考慮工廠方法,但是這又必須根據用戶訪問數據來切換。國家代碼基本上來自其憑據。話雖如此,數據庫已經存在,因此我不得不將它們映射爲數據庫已經結構化......長話短說,儘管我同意命名,但它的目的也是這樣。 – MKunstman