简体   繁体   English

EF保存到上下文但不保存到数据源

[英]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.

相关问题 EF4在保存更改之前看不到上下文的更改 - EF4 Not Seeing Changes on context before Save Changes 如何在EF上下文中保存对象并检查其是否存在,而不将其保存到数据库? - How to save an object in EF context and check if it exists, without saving it to the database? 在EF中动态设置数据源 - Dynamicly set the datasource in EF 将EF ObjectContext的更改克隆到另一个新的上下文中,以安全地异步保存更改 - Cloning the changes of an EF ObjectContext to another new context to save changes async safely 如何使用EF DB上下文在ViewModel中保存对象列表? - How do i save a list of objects in my ViewModel using EF DB Context? 使用EF导入数据。 尽管存在无效(抛出异常),context.saveChanges()如何保存有效的? - Importing data with EF. How context.saveChanges() could save the valid ones, despite there are invalids (throwing Exception)? 如何在EF代码第6版中在上下文中保存枚举 - How can i save an enum in my context in EF code first version 6 代码优先EF保存扩展的实体>>实体类型xxx不是当前上下文模型的一部分 - Code First EF save extended entity >> The entity type xxx is not part of the model for the current context 创建ThreadLocal EF上下文 - Creating a ThreadLocal EF context EF Change数据库上下文 - EF Change Database context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM