我想出了一种方法来实现这一点:
public class Db : DbContext {
private static SqlCeConnectionFactory factory =
new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
public static Db Create(string filename) {
return new Db(factory.CreateConnection(@"Data Source=" + filename));
}
public Db(DbConnection conn)
: base(conn, true) {
}
public DbSet<object> TableName {get; set;}
}
在 unittest 代码中,我可以使用:
private Db CreateDb() {
return Db.Create("R:\\"+TestContext.FullyQualifiedTestClassName +
TestContext.TestName + ".sdf");
}
[TestMethod]
public void StoreAndRetrieve() {
using (var db = CreateDb()) {
...
}
}