Search code examples
c#.netentity-frameworkobjectcontext

EF save to Context but not to Datasource


Having a EF Context and a Testenity I want to get the following test to work.

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

I know that it works with SaveChanges() but I do not want to save the Entity to the datasource.


Solution

  • 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!");