12

What I want to achieve is the simple sql query: UPDATE TABLE SET COLUMN = COLUMN + 1

Is there a way to make it happen without loading all records (thousands) to memory first and loop through each record to increment the column and then save it back?

EDIT

I tried raw sql and it worked. I have to decide the sql provider from the connection string and the database schema name from the connection context. After that, I will use the corresponding sql query to update the table.

For SQL, it looks like UPDATE schemaname.TABLE SET COLUMN = COLUMN + 1. for POSTGRESQL, I have to double quote schema name, table name and column name: UPDATE "schemaname"."TABLE" SET "COLUMN" = "COLUMN" + 1.

5
  • 1
    What is the issue with running the SQL command? (This question is tagged "sql") Commented Jun 3, 2015 at 0:58
  • myContext.Database.ExecuteSqlCommand("UPDATE TABLE SET COLUMN = COLUMN + 1"); Commented Jun 3, 2015 at 1:01
  • I think this does what you want: github.com/MikaelEliasson/… Commented Jun 3, 2015 at 4:06
  • I want to support mssql and npgsql together
    – ydou
    Commented Jun 3, 2015 at 6:06
  • I'm surprised that you couldn't get the raw SQL to work with both DBMS. Care to tell us which one failed and with what error? I am sure someone will be able to help you with that if all else fails.
    – sstan
    Commented Jun 3, 2015 at 18:52

3 Answers 3

4

Here is the solution. You can use the following code:

context.Table.Where(x => x.Field1 > 0).Update(y => new Table { Field2 = y.Field2 + 1 });

hope that it helps.

2
  • 1
    This is EF Core only
    – Proviste
    Commented Feb 25, 2018 at 20:24
  • I can't see this solution in EF Core. How can I do that? Commented Jan 30, 2021 at 9:09
2

With pure EF, you are right: you have to load the entities one by one, set the property, and save. Very inefficient.

The 2 alternatives that I know of are:

  1. Provide raw SQL to execute the UPDATE statement as you want it.
  2. Use the EntityFramework.Extended library (https://github.com/loresoft/EntityFramework.Extended), which supports both bulk UPDATEs and DELETEs with a natural EF feel.

Quote from their main page:

Batch Update and Delete

A current limitations of the Entity Framework is that in order to update or delete an entity you have to first retrieve it into memory. Now in most scenarios this is just fine. There are however some senerios where performance would suffer. Also, for single deletes, the object must be retrieved before it can be deleted requiring two calls to the database. Batch update and delete eliminates the need to retrieve and load an entity before modifying it.

Deleting

//delete all users where FirstName matches
context.Users.Where(u => u.FirstName == "firstname").Delete();

Update

//update all tasks with status of 1 to status of 2
context.Tasks.Update(
    t => t.StatusId == 1, 
    t2 => new Task {StatusId = 2});

//example of using an IQueryable as the filter for the update
var users = context.Users.Where(u => u.FirstName == "firstname");
context.Users.Update(users, u => new User {FirstName = "newfirstname"});
1
  • 1
    EntityFramework.Extended will not help. It's a great library but the batch feature only lets you set columns of multiple records to the same value.
    – Rudey
    Commented Feb 29, 2016 at 6:22
0

You can use EFCore.BulkExtensions in EFCore, solution like this answer https://stackoverflow.com/a/42368027/8163839

context.Table.Where(x => x.Field1 > 0).BatchUpdate(y => new Table { Field2 = y.Field2 + 1 });

There is one small problem, the method parameters are Expression<Func<Table, Table>>, so you can only assign values to properties like above.

It is not DDD friendly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.