在下面的单元测试中,我创建一个存储库对象,并使用InMemoryContext保存更改。然后我 修改对象,但不保存更改。当我从存储库请求对象时 它显示了即使没有保存任何更改,我对对象所做的修改。
当我创建一个新的InMemoryContext并查询相同的对象时,我得到的对象没有任何更改。
为什么InMemoryContext决定我从存储库请求的对象实际上是该对象,而不是存储库?可以将其设置为不具有这种行为吗?
public void EFMeditechItsOrdersRepository_ContextTest1()
{
var orderId = 0;
using (var context = TestingUtils.GetInMemoryContext())
{
var order1 = new Orders
{
CreatedOn = DateTime.Now,
Cancelled = false,
Procedure = "SOMEPOC"
};
var eFOrdersRepo1 = new EFOrderRepository(context);
eFOrdersRepo1.Insert(order1); //Save the changes
orderId = order1.Id;
order1.Procedure = "HUHUH"; //Update to the object but not saving the changes
var order2A = eFOrdersRepo1.Get(orderId);
Assert.IsTrue(order2A.Procedure == "HUHUH"); //Why does this have the change when not saved???
}
using (var context2 = TestingUtils.GetInMemoryContext())
{
var eFOrdersRepo2 = new EFOrderRepository(context2);
var order2B = eFOrdersRepo2.Get(orderId);
Assert.IsTrue(order2B.Procedure == "SOMEPOC"); //Correct content
}
}
}