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

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

问题描述

我想要针对内存数据库运行我的EF4.1存储库的实际集成测试a la 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.

我有一个代码第一个模型,与旧数据库我们希望能够使用Sqlite(或其他)来:

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


$ b

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


  1. 从我的模型生成内存数据库

  2. 使用此内存数据库为我的模型创建一个DBContext
  3. li>
  4. 我已经建立了一个IDBContextFactory的IoC / DI,它通过我的(Generic)存储库(也可以使用GenericRepository模式)构建。

  1. Generate an in-memory database from my model
  2. Create a DBContext for my model with this in-memory database
  3. I have already in place IoC/DI of a IDBContextFactory which gets constructed with my (Generic) Repositories (also using a GenericRepository pattern)

有一些位和bobs在线,这表明应该是可能的,但对于代码优先的方法不是太多。任何人都知道这是否可行?

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 SqlCE和工具由Web平台安装程序) - EF代码首先有直接的支持。唯一的区别是,您的应用程序将使用大型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天全站免登陆