Update statement with Entity Framework

主宰稳场 提交于 2019-12-01 04:05:17

Not really under this form no.

You will have to select all entities that match your criteria, foreach over them and update them.

If you are looking for something that will do it right in the DB because your set could be huge, you will have to use SQL directly. (I don't remember if EF has a way to execute UPDATE queries directly the way Linq To SQL does).

Use the Batch Update feature of the Entity Framework Extended Library, like this:

dbContext.Tests.Update(t => t.Id == 10, t => new Test() { Value = t.Value + 1 });

It should be, it will just be a little bit more constrained generally.

var myEntity = context.First(item => item.id == 10);
myEntity.value += 1;
context.SaveChanges();

Should produce similar SQL, you can watch the profiler to see what SQL is actually being generated, but it should be very similar to your statement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!