1
我做了一個小的測試函數,用於創建直接連接到SQL CE 4.0文件的Entity Framework Code-First DbContext實例,但我真的不喜歡全局狀態方法。沒有更好的方法來做到這一點,而不使用DbDatabase靜態屬性?使用Entity Framework w/o全局狀態
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Database;
using System.Data.SqlServerCe;
public class SqlCeDb
{
public static T Instance<T>() where T: DbContext, new()
{
DbDatabase.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
DbDatabase.SetInitializer<T>(new DropCreateDatabaseAlways<T>());
return new T();
}
}
明白了。看到我擴展的DbContext有一個接受DbConnection的構造函數,它或多或少地解決了這個問題。 – 2011-02-18 18:28:23