1
我想我錯過了一些東西非常簡單這裏。ObjectSet查詢不返回添加EntityObject直到SaveChanges()調用
我有一個EF4 ObjectContext,它包含一個Thing類型的ObjectSet,它映射到我的數據庫中名爲Things的表中。如果我將東西添加到事物ObjectSet中,那麼在我調用ObjectContext上的SaveChanges()之前,東西在東西中不可見。
因此,在下面的測試夾具,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並讓它們可見而不保存到數據庫?