本文介绍了在Entity Framework中附加集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体框架,我可以使用

  entity.Attach(); 

但是,我看到没有任何方法允许我将多个对象的集合/数组添加到实体



我必须循环遍历集合中的每个项目,并每次调用 entity.Attach() p>

解决方案

是的,您必须循环遍历子集,并且 Attach 另外,请注意,如果实体被修改, Attach 将其附加到上下文未修改状态。您还必须将状态设置为修改



参见这里:


Using the entity framework, I am able to attach single objects using

entity.Attach();

However, I see no method that allows me to add a collection / array of multiple objects to an entity.

Must I loop through each item in the collection and call entity.Attach() each time?

解决方案

Yes, you must loop through the child collection and Attach.

Also, take note that if the entity is modified, Attach attaches it to the context Unmodified state. You must also set the state to Modified.

See here: http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

这篇关于在Entity Framework中附加集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 17:02