EF save to context, but not to data source

Having an EF context and testing, 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 Entity in datasource .

+4
source share
2 answers
 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!"); 
+4
source

What are you trying to achieve with your test? All this test will be achieved here to validate the underlying Entity Framework ContextObject, which is meaningless to you.

If you are trying to run additional tests for which ObjectContext is a dependency, you can mock ObjectContext with an interface. We create our own IObjectContext user interface, which we extract from the T4 generated by ContextObject from our model.

-1
source

All Articles