在ef中向Context添加对象列表 [英] adding List of objects to Context in ef
本文介绍了在ef中向Context添加对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在不使用foreach addObject的情况下将实体列表添加到实体框架中的上下文中?
Is it possible to add list of object to Context in entity framework without using foreach addObject ?
感谢帮助
推荐答案
通常您不能这样做-您必须循环执行。但是,在某些情况下,可以避免添加每个对象-特别是如果您有实体图并添加父节点。例如。如果您有一个 Company
对象,该对象具有 Employees
的集合:
Generally you can't do that - you have to do it in a loop. In some cases, however, you can avoid adding every object - specifically, if you have an entity graph and you add the parent node. E.g. if you have a Company
object that has a collection of Employees
:
context.AddToCompanies(company);
/* The following loop is not necessary */
/* The employees will be saved together with the company */
/*
foreach (var employee in company.Employees)
{
context.AddToEmployees(employee);
}*/
context.SaveChanges();
这篇关于在ef中向Context添加对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文