C#实体框架:使用Where和Join进行批量更新 [英] C# Entity Framework: Bulk Update with Where and Join

查看:200
本文介绍了C#实体框架:使用Where和Join进行批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Entity Framework Core中使用Join和Where子句更新某些记录?使用下面的模板答案(附加/ isModified)? https://stackoverflow.com/a/44247720/12425844


"如果您不想使用SQL语句,则可以使用Attach方法来更新实体,而不必先加载它。由于默认情况下其附件为不变您需要调用make isModified = true


当前要求:

  var data = _dbContext.Set< Product>()
.Include(c => c.ProductType)
.Where(x => ; x。制造商== ABC公司&&
x.StartYear == 2019&&
x.ProductType.ProductTypeDescription ==电子产品)

数据。制造商= XYZ公司;
数据.StartYear = 2020;
_dbContext.SaveChangesAsync();

目标代码:
必须添加位置并加入以查找特定记录:


https://stackoverflow.com/a/44247720/12425844

 使用(myDbEntities db = new myDbEntities())
{
试试
{
db.Configuration .AutoDetectChangesEnabled = false;

MyObjectEntity entityToUpdate = new MyObjectEntity(){制造商= XYZ公司,起始年份= 2020};
db.Entry(entityToUpdate).Property(e => e.Manufacturer).IsModified = true;
db.Entry(entityToUpdate).Property(e => e.StartYear).IsModified = true;

db.SaveChanges();
}
最后
{
db.Configuration.AutoDetectChangesEnabled = true;
}
}

使用EF Net Core 3.1,


暂时不接受使用Raw sql或EF Extensions的答案,只是本机EF Core

解决方案

如果EF Extensions不在, EFCore.BulkExtensions 呢?

 等待_dbContext.Set< Product>()
。其中(x => x.Manufacturer == ABC Company&
x.StartYear == 2019&&
x.ProductType.ProductTypeDescription ==电子产品)
.BatchUpdateAsync(x =>新产品(){
制造商= ; XYZ Company,
StartYear = 2020});

注意更新是作为原始sql执行的,加载到变更跟踪器中的对象不会更新。您可能希望在交易中包含批量操作和 SaveChangesAsync


YMMV,我自己没有使用过此库。 / p>

How do I update Certain records in with Join and Where clause in Entity Framework Core? Using the template answer below (Attach/isModified)? https://stackoverflow.com/a/44247720/12425844

"If you don't want to use an SQL statement, you can use the Attach method in order to update an entity without having to load it first. As it's attached as Unchanged by default. You need to call make isModified = true"

Current requirement:

var data = _dbContext.Set<Product>()
    .Include(c => c.ProductType)
    .Where(x => x.Manufacturer == "ABC Company" &&
                x.StartYear == 2019 &&
                x.ProductType.ProductTypeDescription == "Electronics")

data.Manufacturer = "XYZ Company";
data.StartYear = 2020;
_dbContext.SaveChangesAsync();

Goal Code: Have to add where and join to find particular records:

https://stackoverflow.com/a/44247720/12425844

using (myDbEntities db = new myDbEntities())
{
    try
    {
      db.Configuration.AutoDetectChangesEnabled = false;

      MyObjectEntity entityToUpdate = new MyObjectEntity() {Manufacturer = "XYZ Company", StartYear =2020};
      db.Entry(entityToUpdate).Property(e => e.Manufacturer).IsModified = true;
      db.Entry(entityToUpdate).Property(e => e.StartYear).IsModified = true;

      db.SaveChanges();
    }
    finally
    {
      db.Configuration.AutoDetectChangesEnabled = true;
    }
}

Using EF Net Core 3.1 ,

Will not accept answers using Raw sql or EF Extensions for now, just Native EF Core

解决方案

If EF Extensions is out, what about EFCore.BulkExtensions?

await _dbContext.Set<Product>()
    .Where(x => x.Manufacturer == "ABC Company" &&
                x.StartYear == 2019 &&
                x.ProductType.ProductTypeDescription == "Electronics")
    .BatchUpdateAsync(x => new Product(){
                Manufacturer = "XYZ Company",
                StartYear = 2020 });

Note update is performed as raw sql, objects loaded into the change tracker are not updated. You may wish to surround the bulk operations and SaveChangesAsync in a transaction.

YMMV, I haven't used this library myself.

这篇关于C#实体框架:使用Where和Join进行批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆