Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update statement with Entity Framework

Simple question, is it possible to achieve this query this with Entity Framework when updating one entity?

update test set value = value + 1 where id = 10
like image 545
Drevak Avatar asked Mar 16 '09 20:03

Drevak


People also ask

How do I write an update query in Entity Framework?

We can update records either in connected or disconnected scenarios. In the connected Scenario, we open the context, query for the entity, edit it, and call the SaveChanges method. In the Disconnected scenario, we already have the entity with use. Hence all we need to is to attach/add it to the context.

How do I add a new record in Entity Framework?

Use the DbSet. Add method to add a new entity to a context (instance of DbContext ), which will insert a new record in the database when you call the SaveChanges() method. In the above example, context.


1 Answers

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 });
like image 188
Edward Brey Avatar answered Sep 23 '22 22:09

Edward Brey