C# 使用EntityFramework 6和Linq对对象列表进行批更新
我想创建一个函数,该函数接受已设置属性的对象列表,并且只对数据库进行一次调用,即可对这些对象执行批更新或插入操作 (更新)以下是工作版本:C# 使用EntityFramework 6和Linq对对象列表进行批更新,c#,linq,entity-framework,linq-to-sql,C#,Linq,Entity Framework,Linq To Sql,我想创建一个函数,该函数接受已设置属性的对象列表,并且只对数据库进行一次调用,即可对这些对象执行批更新或插入操作 (更新)以下是工作版本: public static void BatchInsertProducts(IList<Product> productList) { using (var context = new DbContext()) { context.Products.AddRange(pro
public static void BatchInsertProducts(IList<Product> productList)
{
using (var context = new DbContext())
{
context.Products.AddRange(productList);
context.SaveChanges();
}
}
公共静态void BatchInsertProducts(IList产品列表)
{
使用(var context=new DbContext())
{
context.Products.AddRange(productList);
SaveChanges();
}
}
这是我尝试根据Id对多个项目执行and update或insert操作的结果。我不认为我可以在此处使用AddRange,因此我尝试了这种方法,但出现了一个错误:
public static void SaveMultipleProducts(IList<Product> productList)
{
using (var context = new DbContext())
{
foreach (Account p in productList)
{
if (p.ID == 0)
{
context.Entry(p).State = EntityState.Added;
p.InsertUserId = "jtunney";
p.InsertDate = DateTime.Now;
}
else
{
context.Entry(p).State = EntityState.Modified;
p.UpdateUserId = "jtunney";
p.UpdateDate = DateTime.Now;
}
}
context.SaveChanges();
}
}
publicstaticvoidsavemultipleproducts(IListproductlist)
{
使用(var context=new DbContext())
{
foreach(产品列表中的帐户p)
{
如果(p.ID==0)
{
context.Entry(p.State=EntityState.Added;
p、 InsertUserId=“jtunney”;
p、 InsertDate=DateTime.Now;
}
其他的
{
context.Entry(p.State=EntityState.Modified;
p、 UpdateUserId=“jtunney”;
p、 UpdateDate=DateTime.Now;
}
}
SaveChanges();
}
}
不必为每个产品调用Save方法。如果获得产品并使用相同的DbContext保存,则只需调用save一次,它就会保存所有修改
想象
List<Product> products = Context.Product.ToList();
foreach(var product in products)
{
product.Price = new Random().Next();
}
Context.Product.SaveChanges();
List products=Context.Product.ToList();
foreach(产品中的var产品)
{
product.Price=new Random().Next();
}
Context.Product.SaveChanges();
这段代码正在修改列表中所有产品的价格,但由于我们使用相同的上下文来检索结果,并且只需调用SaveChanges就可以保存修改
用于批量插入件
使用实体框架中的AddRange方法,或者您可以尝试使用此库。我听说过,但我从未使用过它没有必要为每个产品调用Save方法。如果获得产品并使用相同的DbContext保存,则只需调用save一次,它就会保存所有修改 想象
List<Product> products = Context.Product.ToList();
foreach(var product in products)
{
product.Price = new Random().Next();
}
Context.Product.SaveChanges();
List products=Context.Product.ToList();
foreach(产品中的var产品)
{
product.Price=new Random().Next();
}
Context.Product.SaveChanges();
这段代码正在修改列表中所有产品的价格,但由于我们使用相同的上下文来检索结果,并且只需调用SaveChanges就可以保存修改
用于批量插入件
使用实体框架中的AddRange方法,或者您可以尝试使用此库。我听说过,但我从未使用过它没有必要为每个产品调用Save方法。如果获得产品并使用相同的DbContext保存,则只需调用save一次,它就会保存所有修改 想象
List<Product> products = Context.Product.ToList();
foreach(var product in products)
{
product.Price = new Random().Next();
}
Context.Product.SaveChanges();
List products=Context.Product.ToList();
foreach(产品中的var产品)
{
product.Price=new Random().Next();
}
Context.Product.SaveChanges();
这段代码正在修改列表中所有产品的价格,但由于我们使用相同的上下文来检索结果,并且只需调用SaveChanges就可以保存修改
用于批量插入件
使用实体框架中的AddRange方法,或者您可以尝试使用此库。我听说过,但我从未使用过它没有必要为每个产品调用Save方法。如果获得产品并使用相同的DbContext保存,则只需调用save一次,它就会保存所有修改 想象
List<Product> products = Context.Product.ToList();
foreach(var product in products)
{
product.Price = new Random().Next();
}
Context.Product.SaveChanges();
List products=Context.Product.ToList();
foreach(产品中的var产品)
{
product.Price=new Random().Next();
}
Context.Product.SaveChanges();
这段代码正在修改列表中所有产品的价格,但由于我们使用相同的上下文来检索结果,并且只需调用SaveChanges就可以保存修改
用于批量插入件
使用实体框架中的AddRange方法,或者您可以尝试使用此库。我听说过,但从未使用过它删除每个循环,将
IList
更改为IEnumerable
:
公共静态void BatchInsertProducts(IEnumerable productList)
{
使用(var context=new DbContext())
{
context.Products.AddRange(productList);
SaveChanges();
}
}
保存多个文件的一种方法:
public static void SaveMultipleProducts(IEnumerable<Product> productList)
{
using (var context = new DbContext())
{
foreach (Account p in productList)
{
p.InsertUserId="jtunney";
p.InsertDate=DateTime.Now;
context.Entry(p).State=p.Id==0?EntityState.Added:EntityState.Modified;
}
context.SaveChanges();
}
}
publicstaticvoidsavemultipleproducts(IEnumerable产品列表)
{
使用(var context=new DbContext())
{
foreach(产品列表中的帐户p)
{
p、 InsertUserId=“jtunney”;
p、 InsertDate=DateTime.Now;
context.Entry(p.State=p.Id==0?EntityState.Added:EntityState.Modified;
}
SaveChanges();
}
}
另一种方式:
public static void SaveMultipleProducts(IList<Product> productList)
{
using (var context = new DbContext())
{
foreach (Account p in productList)
{
p.InsertUserId="jtunney";
p.InsertDate=DateTime.Now;
}
// Add all records
context.Products.AddRange(productList);
// Handle updates
foreach(var p in productList.Where(p=>p.id!=0))
{
context.Entry(p).State=EntityState.Modified;
}
context.SaveChanges();
}
}
publicstaticvoidsavemultipleproducts(IListproductlist)
{
使用(var context=new DbContext())
{
foreach(产品列表中的帐户p)
{
p、 InsertUserId=“jtunney”;
p、 InsertDate=DateTime.Now;
}
//添加所有记录
context.Products.AddRange(productList);
//处理更新
foreach(productList.Where中的var p(p=>p.id!=0))
{
context.Entry(p.State=EntityState.Modified;
}
SaveChanges();
}
}
删除了foreach循环,将IList
更改为IEnumerable
:
公共静态void BatchInsertProducts(IEnumerable productList)
{
乌辛