其实下文中说的解决方案不仅仅适用于应用了Ef的项目,其它涉及数据访问的测试同样适用。

不说先贴上原文链接 :

(一) http://graemehill.ca/unit-testing-an-entity-framework-data-access-layer-part-1-just-hit-the-database

(二)http://graemehill.ca/unit-testing-an-entity-framework-data-access-layer-part-2-rolling-back-the-test-database

(三) http://graemehill.ca/high-performance-database-rollback-in-automated-tests-with-sql-server

A couple months ago I wrote this article explaining why I think it is reasonable for unit tests to hit a real database. Subsequently, I wrote a follow up article describing some techniques for rolling back your database to its original state after each test. In that article I found that just using simple transactions did not solve the problem because you need access to all database connections being used, and they all have to be rolled back. I have since found a way around this problem using distributed transactions.

With the Microsoft Distributed Transaction Coordinator (MSDTC) the activity over multiple connections can be lumped into a single transaction using the TransactionScope class. MSDTC needs to be running for this to work, but since this is just for unit tests it doesn’t need to be enabled on your production environment.

In order to use the TransactionScope class your project will need a reference to System.Transactions. Here’s a sample unit test using MSTest and Entity Framework where the database is altered with multiple connections within a transaction and then the changes are rolled back:

Imports System.Transactions Imports System Imports System.Text Imports System.Collections.Generic Imports Microsoft.VisualStudio.TestTools.UnitTesting   <TestClass()> _ Public Class UnitTestSample       <TestMethod()> _     Public Sub ProofOfConceptTest()         Using New TransactionScope             Dim conn1 As New DataTestEntities             Dim conn2 As New DataTestEntities               Dim row1 As New Users With {.userName = "user1", .password = "pass"}             Dim row2 As New Users With {.userName = "user2", .password = "pass"}               conn1.AddToUsers(row1)             conn2.AddToUsers(row2)               conn1.SaveChanges()             conn2.SaveChanges()               Dim conn3 As New DataTestEntities             Assert.AreEqual(conn3.Users.Count, 6)         End Using     End Sub   End Class

Alternatively, if you want every test method inside a test class to be within its own TransactionScope without adding a Using block to every single test, you can use the initialization and cleanup methods like this:

Imports System.Transactions Imports System Imports System.Text Imports System.Collections.Generic Imports Microsoft.VisualStudio.TestTools.UnitTesting   <TestClass()> _ Public Class UnitTestSample       Private _transaction As TransactionScope       <TestInitialize()> _     Public Sub Setup()         _transaction = New TransactionScope     End Sub       <TestCleanup()> _     Public Sub TearDown()         _transaction.Dispose()     End Sub       <TestMethod()> _     Public Sub ProofOfConceptTest()         Dim conn1 As New DataTestEntities         Dim conn2 As New DataTestEntities           Dim row1 As New Users With {.userName = "user1", .password = "pass"}         Dim row2 As New Users With {.userName = "user2", .password = "pass"}           conn1.AddToUsers(row1)         conn2.AddToUsers(row2)           conn1.SaveChanges()         conn2.SaveChanges()           Dim conn3 As New DataTestEntities         Assert.AreEqual(conn3.Users.Count, 6)     End Sub   End Class

As long as the use of MSDTC is an option, I have found this method to be far better than any of those described in the last article. It guarantees that the state or your database is maintained and is extremely fast (at least on small amounts of data).

转载于:https://www.cnblogs.com/jinzhao/archive/2011/07/25/High-performance-database-rollback-in-automated-tests-with-SQL-Server.html

Entity Framework Unit Testing problem and solution(转)相关推荐

  1. 在Entity Framework 4.0中使用 Repository 和 Unit of Work 模式

    [原文地址]Using Repository and Unit of Work patterns with Entity Framework 4.0  [原文发表日期] 16 June 09 04:0 ...

  2. Entity Framework context per request

    原文发布时间为:2011-09-24 -- 来源于本人的百度文章 [由搬家工具导入] http://www.blog.cyberkinetx.com/2011/05/15/entity-framewo ...

  3. ASP.NET Core 中的 ORM 之 Entity Framework

    目录 EF Core 简介 使用 EF Core(Code First) EF Core 中的一些常用知识点 实体建模 实体关系 种子数据 并发管理 执行 SQL 语句和存储过程 延迟加载和预先加载 ...

  4. 如何使用Entity Framework在Always Encrypted列上模拟通配符搜索

    介绍 (Introduction) The title of this post should have been "How to implement wildcard search fun ...

  5. 《Pragmatic Unit Testing In Java with JUnit》—单元测试之道读后感

    <Pragmatic Unit Testing In Java with JUnit>                                                    ...

  6. [转]Porting to Oracle with Entity Framework NLog

    本文转自:http://izzydev.net/.net/oracle/entityframework/2017/02/01/Porting-to-Oracle-with-Entity-Framewo ...

  7. 【配置映射】—Entity Framework实例详解

    前两篇博文中的配置属性和配置关系都是配置映射,配置属性是属性的映射,配置关系式关系的映射,本篇从讲讲实体的映射. 首先,配置实体映射到表,使用ToTable方法,它接受两个参数,第一个参数是表的名称, ...

  8. EFMVC - ASP.NET MVC 3 and Entity Framework 4.1 Code First 项目介绍

    项目概述 使用ASP.NET MVC 3.Razor.EF Code First.Unity 2.0 等等技术,演示如何创建一个ASP.NET MVC 3 的范例应用程序. 相关技术帖子: 中文: 使 ...

  9. ADO.NET Entity Framework 学习(1) [ZT]

    前一段时间园子里有很多文章介绍分析了Linq To SQL,它可以说是一个简单实现的ORM,是微软针对数据和对象的阻抗不平衡的问题.C# 3.0推出一些新的特性,比如Extension Method, ...

最新文章

  1. 大剑无锋之浅析Cookie/Session/Token
  2. Ajax同步和异步的区别?
  3. lasso特征选择python_转:结合Scikit-learn介绍几种常用的特征选择方法-2
  4. java stream 取不同的数据_基础篇:JAVA.Stream函数,优雅的数据流操作
  5. viewport就是这回事
  6. 自动绕过锐捷客户端开热点
  7. java 银行帐号_Java 根据银行卡号获取银行名称以及图标
  8. JavaScript特效之显示当前时间
  9. 调用webservice服务方式总结
  10. 模拟电子线路复习笔记( 六) —— 集成运算放大器原理及其运用
  11. spring-05 AOP
  12. 计算机病毒模块测试题,计算机病毒分类测试题集
  13. 我是屌丝——彪悍的人生不需要代言
  14. dw中css目标规则命名,CSS名规则.doc
  15. matlab报错slcc_interface_z6c0T9v%^%$
  16. PPT中建立打开Onenote特定笔记本的超链接
  17. 水果店促销方案,水果店开业怎么搞促销
  18. 微信小程序趋势及前景,详细的Android学习指南
  19. ​鼠去仓实谷稻熟,牛来福地歌丰年
  20. 【C++】STL —— unordered_map/unordered_set的基本使用

热门文章

  1. docker mysql镜像连接不上_Mysql安装-Docker版
  2. 数据库授予用户增删改查的权限的语句_mysql创建本地用户及赋予数据库权限的方法示例...
  3. webpack4.x开发环境配置
  4. pythondjango项目集成_[Python]将Wagtail整合到Django2项目中
  5. protoc.exe java_protobuf java 使用 window
  6. c语言从串口获取数据,如何通过串口来读写数据,请教达人
  7. 用opencv的traincascade.exe训练行人的HAAR、LBP和HOG特征的xml
  8. Hello World With JBoss Modules
  9. 老手是这样教新手编程的
  10. 从代码层读懂Java HashMap的实现原理