好的,現在問題似乎已經解決了。我正在使用DbConnections。
public MyContext() : base(ConnectionManager.Connection, true)
{
Database.SetInitializer<MyContext>(new MyContextInitializer());
Configuration.ProxyCreationEnabled = false;
}
public MyContext(DbConnection connection) : base(connection, true)
{
Database.SetInitializer<MyContext>(new MyContextInitializer());
Configuration.ProxyCreationEnabled = false;
}
我在一個特殊的類中創建了DbConnection,我認爲這不適合在這裏發佈代碼。但它基本上是這樣的:
DbConnection conn = null;
switch (Type)
{
case ConnectionType.LocalDB:
conn = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection();
break;
case ConnectionType.MySql:
conn = DbProviderFactories.GetFactory("MySql.Data.MySqlClient").CreateConnection();
break;
default:
throw new System.InvalidOperationException();
}
conn.ConnectionString = "Add provider specific connection string here";
然後,你只需要將代碼somhow給上下文。在我的情況下,我有一個ConnectionManager,當我調用MyContext()時讀取「默認連接」,並且還有第二個Ctor,我稱它爲「測試連接」。
謝謝你,我會考慮使用你的soluition,看起來相當不錯:) – SirBirne 2015-02-17 10:10:14