Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在单元测试项目中使用EntityFramework5回滚更改_C#_Entity Framework_Unit Testing_Visual Studio 2012_Entity Framework 5 - Fatal编程技术网

C# 如何在单元测试项目中使用EntityFramework5回滚更改

C# 如何在单元测试项目中使用EntityFramework5回滚更改,c#,entity-framework,unit-testing,visual-studio-2012,entity-framework-5,C#,Entity Framework,Unit Testing,Visual Studio 2012,Entity Framework 5,我正在玩实体框架,我有一个单元测试项目,我想练习我到目前为止所做的。我希望它在完成后不会真正更新我的测试数据库。如果我使用SQL,我会创建一个事务,然后在最后回滚它 我怎么能在这里做同样的事情 据我所知,context.SaveChanges()有效地对数据库进行写入。如果没有,则在分配context.CarTypes.ToList()后,allcartypes为空 下面是我的一个测试类的示例 using System; using System.Diagnostics; using Micro

我正在玩实体框架,我有一个单元测试项目,我想练习我到目前为止所做的。我希望它在完成后不会真正更新我的测试数据库。如果我使用SQL,我会创建一个事务,然后在最后回滚它

我怎么能在这里做同样的事情

据我所知,
context.SaveChanges()有效地对数据库进行写入。如果没有,则在分配
context.CarTypes.ToList()后,
allcartypes
为空

下面是我的一个测试类的示例

using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Trains;
using System.Linq;

namespace TrainsTest
{
    [TestClass]
    public class TestCarType : TestBase
    {
        [TestMethod]
        public void TestCarTypeCreate_Success()
        {
            var tankerCarType = new CarType {Name = "Tanker"};
            var boxCarType = new CarType { Name = "Box" };
            using (var context = new TrainEntities())
            {
                context.CarTypes.Add(tankerCarType);
                context.CarTypes.Add(boxCarType);

                context.SaveChanges();

                var allCartTypes = context.CarTypes.ToList();
                foreach (var cartType in allCartTypes)
                {
                    Debug.WriteLine(cartType.CarTypeId + " - " + cartType.Name);
                }
            }
        }
    }
}

我知道我遗漏了一些基本的东西,但我不知道是什么。我的谷歌搜索也没有结果。

有一篇关于ef交易的MSDN文章