【问题标题】:Entity Framework Update from List Of class从类列表更新实体框架
【发布时间】:2018-02-02 09:25:57
【问题描述】:

我有下表CustEntry:-

CustID CustName CustAddress AddedBy DateAdded CustStatus

在这张表中我有很多记录。

我正在使用 EF6 在 entityframework 中,我只想更新所有表记录的 2 列。

为此,我的类结构为:-

class CustEntry
{
    public string CustName {get;set;}
    public string CustStatus {get;set;}
}
class RootCustEntry
{
    public List<CustEntry> CustEntry{ get; set; }
}

注意:- 我只需要更新通过 RootCustEntry 对象中的 List CustEntry 传递给我的 CustName 和 CustStatus

如何在不使用 foreach 循环的情况下批量更新记录?

【问题讨论】:

    标签: c# asp.net .net entity-framework


    【解决方案1】:

    如何在不使用 foreach 循环的情况下批量更新记录?

    不幸的是,实体框架不支持开箱即用。

    但是,您可以使用EntityFramework.Extended 中的批量更新功能(自 2015 年起不支持,如其 github 所述)。

    因此您可以查看此EntityFramework Plus(由 EntityFramework.Extended 的创建者推荐,如其 github 上所述)。

    Batch Update Example code of EntityFramework Plus

    // using Z.EntityFramework.Plus; // Don't forget to include this.
    
    // UPDATE all users inactive for 2 years
    var date = DateTime.Now.AddYears(-2);
    ctx.Users.Where(x => x.LastLoginDate < date)
             .UpdateAsync(x => new User() { IsSoftDeleted = 1 });
    

    PS:如果你不想使用 3rd 方库,那么你可以写一个stored procedure

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-25
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多