拥有 EF 上下文和 Testenity 我想让以下测试工作。

TestEntity testEntity = new TestEntity() { Name = "Hello World" };
context.TestEntities.AddObject(testEntity);
// missing Code
Assert.AreEqual(1, context.TestEntities.Count(), "Entity not in context!");

我知道它适用于 SaveChanges() 但我确实 不想将 实体保存到 数据源

最佳答案

TestEntity testEntity = new TestEntity() { Name = "Hello World" };
context.TestEntities.AddObject(testEntity);

var entitiesInOSM = context.ObjectStateManager.
        GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged).
        Where(ent => ent.Entity is TestEntity).
        Select(ent => ent.Entity as TestEntity);

Assert.AreEqual(1, entitiesInOSM.Count(), "Entity not in context!");

关于c# - EF 保存到上下文但不保存到数据源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3404637/

10-09 06:43