加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite – 集成测试实体框架代码首先使用内存数据库

发布时间:2020-12-12 18:58:36 所属栏目:百科 来源:网络整理
导读:我想对内存数据库a la ayende’s nhibernate version运行我的EF4.1存储库的实际集成测试. 我有一个代码优先模型,针对遗留数据库(旧表和列名称需要使用代码配置映射到我的entites). 我希望能够使用Sqlite(或其他)来: 从我的模型生成内存数据库 使用此内存数
我想对内存数据库a la ayende’s nhibernate version运行我的EF4.1存储库的实际集成测试.

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

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

>从我的模型生成内存数据库
>使用此内存数据库为我的模型创建DBContext
>我已经使用了我的(通用)存储库(也使用GenericRepository模式)构建的IDBContextFactory的IoC / DI

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

我的测试库的一些片段,请参阅// 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>();
    }

解决方法

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读