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

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

概述我想对内存数据库a la ayende’s nhibernate version运行我的EF4.1存储库的实际集成测试. 我有一个代码优先模型,针对遗留数据库(旧表和列名称需要使用代码配置映射到我的entites). 我希望能够使用Sqlite(或其他)来: >从我的模型生成内存数据库 >使用此内存数据库为我的模型创建DBContext >我已经使用了我的(通用)存储库(也使用GenericRep 我想对内存数据库a la ayende’s nhibernate version运行我的EF4.1存储库的实际集成测试.

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

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

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

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

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

@H_404_23@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; } } }

@H_404_23@[Test] public voID test_me() { var auditRespository = new AuditRepository(new MyDbContextFactory()); auditRespository.GetAll<Audit>(); }解决方法 使用sql Compact 4.0(通过Web平台安装程序下载sqlCE和工具) – EF Code首先直接支持.唯一的区别是您的应用程序将使用连接字符串到大sql Server:

@H_404_23@<add name="MyDbContext" provIDer="System.Data.sqlClIEnt" connectionString= "Data Source=...;InitialCatalog=...;Integrated Security=sspI" />

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

@H_404_23@<add name="MyDbContext" provIDer="System.Data.sqlServerCe.4.0" connectionString="Data Source=Database.sdf" /> 总结

以上是内存溢出为你收集整理的sqlite – 集成测试实体框架代码首先使用内存数据库全部内容,希望文章能够帮你解决sqlite – 集成测试实体框架代码首先使用内存数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/sjk/1156305.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存