[英]EF save to Context but not to Datasource
Having a EF Context and a Testenity I want to get the following test to work. 拥有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!");
I know that it works with SaveChanges() but I do not want to save the Entity to the datasource . 我知道它适用于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!");
What are you trying to achieve with your test? 你想通过测试达到什么目的? All this test would achieve here is to Assert the underlying Entity Framework ContextObject which is a pointless test for you. 所有这些测试都将在这里实现,即断言底层实体框架ContextObject,这对您来说是一个毫无意义的测试。
If you're trying to then run additional tests for which the ObjectContext is a dependency you can mock out the ObjectContext with an interface. 如果您正尝试运行其他ObjectContext是依赖项的其他测试,则可以使用接口模拟ObjectContext。 We create our own custom IObjectContext interface we extract from the T4 generated ContextObject off our model. 我们创建了自己的自定义IObjectContext接口,我们从T4生成的ContextObject中提取出我们的模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.