简体   繁体   English

实体框架5跟踪变化

[英]Entity Framework 5 Track changes

Can I track changes of child entities using ef 5? 我可以使用ef 5跟踪子实体的更改吗?

Example: 例:

Domain objects: Book (one to many) Page 域对象:书(一对多)页面

var oldBook = context.Books.Include("Pages");
context.Entry(oldBook).CurrentValues.SetValues(updatedBook);

This code will update simple properties of the old book object with values of simple properties from updatedBook object. 此代码将使用updatedBook对象中的简单属性值更新旧书对象的简单属性。

It there any way to track children collection?(Pages in this case). 有没有办法跟踪儿童收藏?(在这种情况下的页面)。 Or any best practices how to do it? 或者任何最佳实践如何做到这一点?

Your questions is a bit ambiguous. 你的问题有点含糊不清。

Can I track changes of child entities using ef 5? 我可以使用ef 5跟踪子实体的更改吗?

Of course, unless you explicitly disable change tracking using IQueryable.AsNoTracking() or MergeOption.NoTracking then you can track changes of any entity attached to the DBContext ObjectStateManager . 当然,除非您使用IQueryable.AsNoTracking()MergeOption.NoTracking明确禁用更改跟踪,否则您可以跟踪附加到DBContext ObjectStateManager的任何实体的更改。

If you are really asking if there's a function where you can do this: 如果您真的在询问是否有可以执行此操作的功能:

context.Entry(oldBook).CurrentValues.SetValues(updatedBook);

And have the current values of the entire object graph -- where oldbook is the root -- set to the updated values of the updatedBook object graph then, no. 并且将整个对象图的当前值(其中oldbook是根)设置为updatedBook对象图的更新值,然后,不。

You have to loop through and call context.Entry(oldPage).CurrentValues.SetValues(updatedPage) for each page you wished to update. 您必须循环并为您希望更新的每个页面调用context.Entry(oldPage).CurrentValues.SetValues(updatedPage)

I take it that you are in a disconnected scenario where you can't just pull the entities from the database and use automatic change tracking and set the modified values directly on the tracked entities, otherwise you could just use a single object graph attached to the context instead of working with two instances (ie oldbook, updated book). 我认为你处于一个断开连接的场景,你不能只从数据库中提取实体并使用自动更改跟踪并直接在被跟踪的实体上设置修改后的值,否则你可以使用一个附加到上下文而不是使用两个实例(即旧书,更新的书)。

If you already have a detached modified object graph that you need to work with an alternative to retrieving the entity from the db and using SetValues() is to attach the entities to the context as modified. 如果您已经有一个分离的修改对象图,您需要使用替代方法从数据库中检索实体,并使用SetValues()将实体附加到已修改的上下文。 You still need to loop through and do the same for all entities in the object graph. 您仍然需要遍历并对对象图中的所有实体执行相同操作。

context.Entry(updatedBook).State = EntityState.Modified;
foreach(var p in updatedBook.Pages) context.Entry(p).State = EntityState.Modified;
context.SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架 6:审计/跟踪变更 - Entity Framework 6: audit/track changes 实体框架 - 跟踪更改和回滚 - Entity Framework - Track changes and rollback 缓存实体框架数据并跟踪其变化? - Cache Entity Framework data and track its changes? 重新连接实体时,实体框架不会跟踪集合更改 - Entity Framework does not track collection changes when reconnecting an entity 使用Entity Framework更改跟踪器跟踪对集合的更改 - Track changes to collections using Entity Framework change tracker 为什么Entity Framework在使用存储库模式时不跟踪更改? - Why does Entity Framework not track changes when using repository pattern? 跟踪实体框架进展 - Track Entity Framework Progress 如何使用Entity Framework以DataGridView可编辑和上下文跟踪更改的方式过滤数据? - How to filter data using Entity Framework in a way that DataGridView be editable and context track changes? 我可以使用实体框架来跟踪 DataSet 之类的更改吗(无需连接到数据库)? - Can I use Entity Framework to track changes like DataSet (without connecting to database)? C#实体框架动态代理-维护相同记录的单独对象实例,并且仅跟踪其中的更改? - C# Entity Framework Dynamic Proxy - Maintain Separate Object Instances of Same Record and Only Track Changes in One?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM