我想我在这里错过了很简单的非常。
我有一个EF4 ObjectContext,它包含一个Thing类型的ObjectSet,它被映射到我的数据库中名为Things的表。如果我向Things ObjectSet添加一个Thing,那么在ObjectContext上调用SaveChanges()之前,Thing在Things中是不可见的。
因此,在以下测试夹具中,AddToThings_AddsItemsToContext_WithSaveChanges通过,但AddToThings_AddsItemsToContext_WithoutSaveChanges失败:
[TestFixture]
public class Ef4Tests
{
[SetUp]
public void SetUp()
{
var ef4PocEntities = new Ef4PocEntities();
ef4PocEntities.DeleteDatabase();
ef4PocEntities.CreateDatabase();
}
// this test passes
[Test]
public void AddToThings_AddsItemsToContext_WithSaveChanges()
{
var ef4PocEntities = new Ef4PocEntities();
Assert.AreEqual(0, ef4PocEntities.Things.Count());
ef4PocEntities.AddToThings(new Thing {Name = "Bob"});
ef4PocEntities.SaveChanges();
Assert.AreEqual(1, ef4PocEntities.Things.Count());
}
// this test fails
[Test]
public void AddToThings_AddsItemsToContext_WithoutSaveChanges()
{
var ef4PocEntities = new Ef4PocEntities();
Assert.AreEqual(0, ef4PocEntities.Things.Count());
ef4PocEntities.AddToThings(new Thing {Name = "Bob"});
Assert.AreEqual(1, ef4PocEntities.Things.Count());
}
}
有没有办法做我想要的?即将实体添加到ObjectSet并让它们可见而不保存到数据库?