【发布时间】:2011-05-06 13:27:52
【问题描述】:
我想针对内存数据库运行我的 EF4.1 存储库的实际集成测试,例如 ayende's nhibernate version。
我有一个代码优先模型,针对旧数据库(旧表和列名需要使用代码配置映射到我的实体)。
我希望能够使用 Sqlite(或其他)来:
- 从我的模型生成内存数据库
- 使用这个内存数据库为我的模型创建一个 DBContext
- 我已经设置了 IDBContextFactory 的 IoC/DI,它使用我的(通用)存储库(也使用 GenericRepository 模式)构建
网上有一些信息表明它应该是可能的,但对于代码优先的方法来说并不多。有人知道这是否可能吗?
我的测试库的一些 sn-ps,见 // THROWS ERROR 标记运行时错误:
public class MyDbContextFactory : IDbContextFactory
{
private static object context;
public object CurrentContext
{
get {
if(context == null)
{
// ?? DOESN'T WORK AS THERE'S NO META DATA
var connBuilder = new EntityConnectionStringBuilder();
connBuilder.Provider = "System.Data.SQLite";
connBuilder.Metadata =
@"res://*/TestEfDb.csdl|res://*/TestEfDb.ssdl|res://*/TestEfDb.msl";
connBuilder.ProviderConnectionString =
ConfigurationManager.ConnectionStrings["DataContext"].Name;
var entConnection = new EntityConnection(connBuilder.ConnectionString);
// THROWS ERROR: sqlite Format of the initialization string does not
// conform to specification starting at index 0
// for connection string "Data Source=:memory:;Version=3;New=True;"
//var entConnection = new EntityConnection
// (ConfigurationManager.ConnectionStrings["DataContext"].Name);
context = new MyDbContext(entConnection);
}
return context;
}
}
}
...
[Test]
public void test_me()
{
var auditRespository = new AuditRepository(new MyDbContextFactory());
auditRespository.GetAll<Audit>();
}
【问题讨论】:
-
您多次首先提到代码,但同时您正在构建实体连接字符串,引用来自 EDMX 文件的资源文件(设计器),那么您使用的是哪种方法?
-
quote: "?? DOESNT WORK AS THERES NO META DATA" 它的代码优先
标签: sqlite entity-framework-4.1 ef-code-first