简体   繁体   English

EntityFramework更新与数据库的一次往返

[英]EntityFramework Update with one roundtrip to the database

I have a table as follow: 我有一张桌子,如下所示:

Contacts: 联系人:

| Id | Name  | Age |
|----|-------|-----|
| 1  | amir  | 35  |
| 2  | steve | 29  |

And also have POCO entities (detached entity) as follow: 并且还具有POCO实体(独立实体),如下所示:

var contacts = new [] {
                        Contact {Id = 1, Age = 34},
                        Contact {Id = 2, Age = 25}
                       };

and then: 接着:

using (var ctx = new ApplicationDbContext())
{
    ctx.Contacts.Update(contacts);
    ctx.SaveChanges();
}

What is the best practice to update table rows mentioned at the first with one round-trip database via entity-framework (code first)? 通过实体框架使用一个往返数据库更新最开始提到的表行的最佳实践是什么(代码优先)?
Note: I want to update only Age column. 注意:我只想更新“ 年龄”列。

EF Core supports batching and if you set its batch size to 1, you will get the same result as EF 6.x EF Core支持批处理,如果将其批处理大小设置为1,您将获得与EF 6.x相同的结果

optionsBuilder.UseSqlServer(
   @"Server=(localdb)\mssqllocaldb;Database=Demo.Batching;Trusted_Connection=True;",
   options => options.MaxBatchSize(1)
);

Which means you should start using the EF Core instead of EF 6.x, if you want less round trips to the db for insert/update/delete, or you should use 3dr party libraries which support BulkUpdate such as https://github.com/zzzprojects/EntityFramework-Plus 这意味着,如果您希望减少到数据库的往返行程以进行插入/更新/删除,则应该开始使用EF Core,而不是EF 6.x,或者应该使用支持BulkUpdate 3dr方库,例如https:// github。 com / zzzprojects / EntityFramework-Plus

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

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