首先使用内存数据库集成测试实体框架代码 [英] Integration Testing Entity Framework code first with in-memory database

查看:30
本文介绍了首先使用内存数据库集成测试实体框架代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想针对内存数据库运行我的 EF4.1 存储库的实际集成测试,例如 ayende 的 nhibernate 版本.

I'd like to run actual integration tests of my EF4.1 repositories against an in-memory database a la ayende's nhibernate version.

我有一个代码优先模型,针对旧数据库(旧表和列名称需要使用代码配置映射到我的实体).

I have a code first model, against a legacy database (old table and column names need mapping to my entites using code configurations).

我希望能够使用 Sqlite(或其他)来:

I'd like to be able to use Sqlite (or other) to:

  1. 从我的模型生成一个内存数据库
  2. 使用这个内存数据库为我的模型创建一个 DBContext
  3. 我已经准备好 IDBContextFactory 的 IoC/DI,它是用我的(通用)存储库(也使用 GenericRepository 模式)构建的

网上有一些点点滴滴,这表明它应该是可能的,但对于代码优先的方法来说并不多.有人知道这是否可能吗?

There's bits and bobs on-line which suggest it should be possible, but not much for code-first approaches. Anyone know if this is possible?

我的测试库的一些片段,参见//THROWS ERROR 标记运行时错误:

Some snippets of my test library, see // THROWS ERROR marking runtime errors:

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>();
    }

推荐答案

使用 SQL Compact 4.0(通过 Web 平台安装程序下载 SqlCE 和工具) - EF Code first 对此有直接支持.唯一的区别是您的应用程序将使用连接字符串到大型 SQL Server:

Use SQL Compact 4.0 (download both SqlCE and tools by web platform installer) - EF Code first has direct support for that. The only difference will be that your application will use connection string to big SQL Server:

<add name="MyDbContext" 
     provider="System.Data.SqlClient" 
     connectionString=
       "Data Source=...;InitialCatalog=...;Integrated Security=SSPI" />

并且您的测试将使用连接字符串到 SQL Compact:

and your tests will use connection string to SQL Compact:

<add name="MyDbContext" 
     provider="System.Data.SqlServerCe.4.0" 
     connectionString="Data Source=Database.sdf" />

这篇关于首先使用内存数据库集成测试实体框架代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆