简体   繁体   English

Entity Framework Core(7) 批量更新

[英]Entity Framework Core(7) bulk update

How to bulk update entities using EF Core(7)?如何使用 EF Core(7) 批量更新实体?

I do not want to load entities from the DB server, modify properties and update.我不想从数据库服务器加载实体、修改属性和更新。 I just want to EF generate appropriate UPDATE statement.我只想 EF 生成适当的 UPDATE 语句。

As the accepted answer pointed, Entity Framework Core doesn't support to updates directly in the database yet. 正如所接受的答案所指出的,Entity Framework Core不支持直接在数据库中进行更新。

Disclaimer : I'm the owner of the project Entity Framework Plus 免责声明 :我是项目Entity Framework Plus的所有者

However, EF+ already supports Query Batch Update without loading entities in the context (Support: EF Core, EF6, EF5) 但是,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 });

Wiki: Entity Framework Batch Update Wiki: 实体框架批量更新

This is the EF7 ExecuteUpdateAsync syntax:这是 EF7 ExecuteUpdateAsync语法:

var updatedColumns = 
    await
    context
    .Blog
    .ExecuteUpdateAsync(p => p.SetProperty(s => s.Title, s => "Wow"));

Learn more about bulk operations at What's new on EF7, ExecuteUpdate and ExecuteDelete (Bulk updates) doc. 在 EF7 的新增功能、ExecuteUpdate 和 ExecuteDelete(批量更新)文档中了解有关批量操作的更多信息。

At the day I am posting this answer, with the information in my possession, it looks like it is a work in progress 在我发布这个答案的那天,有了我掌握的信息,看起来它work in progress

See https://github.com/aspnet/EntityFramework/issues/795 请参阅https://github.com/aspnet/EntityFramework/issues/795

EF does not provide a batch update mechanism. EF不提供批量更新机制。 A proposal is below. 提案如下。 Context.Customers.Update().Where.( c => c.CustType ==“New”).Set( x => x.CreditLimit=0) Context.Customers.Update()。Where。(c => c.CustType ==“New”)。Set(x => x.CreditLimit = 0)

Will you consider this feature? 你会考虑这个功能吗? More details here: https://entityframework.codeplex.com/workitem/52 更多详细信息: https//entityframework.codeplex.com/workitem/52

It is released in efcore 7它在 efcore 7 中发布

await context.Blogs.ExecuteUpdateAsync(
    s => s.SetProperty(b => b.Name, b => b.Name + " *Featured!*"));

Generates the appropriate:生成适当的:

UPDATE [b]
    SET [b].[Name] = [b].[Name] + N' *Featured!*'
FROM [Blogs] AS [b]

Examples got from official dos .示例来自官方dos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM