【问题标题】:Entity Framework Core(7) bulk updateEntity Framework Core(7) 批量更新
【发布时间】:2019-10-14 06:06:21
【问题描述】:

如何使用 EF Core(7) 批量更新实体?

我不想从数据库服务器加载实体、修改属性和更新。 我只想 EF 生成适当的 UPDATE 语句。

【问题讨论】:

    标签: c# entity-framework-core


    【解决方案1】:

    正如公认的答案所指出的,Entity Framework Core 还不支持直接在数据库中进行更新。

    免责声明:我是项目的所有者Entity Framework Plus

    不过,EF+ 已经支持查询批量更新,无需在上下文中加载实体(支持:EF Core、EF6、EF5)

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

    维基:Entity Framework Batch Update

    【讨论】:

      【解决方案2】:

      在我发布此答案的那天,根据我拥有的信息,它看起来像是work in progress

      https://github.com/aspnet/EntityFramework/issues/795

      EF 不提供批量更新机制。下面是一个提议。 Context.Customers.Update().Where.( c => c.CustType ==“New”).Set( x => x.CreditLimit=0)

      你会考虑这个功能吗?更多细节在这里: https://entityframework.codeplex.com/workitem/52

      【讨论】:

      • 我明白了,奇怪的是我没有自己google,尝试了很多。
      猜你喜欢
      • 2017-09-17
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 2016-07-29
      • 2016-05-08
      • 2017-02-11
      • 2018-04-07
      相关资源
      最近更新 更多