Entity framework update one column by increasing the current value by one without select

c# entity-framework entity-framework-6 sql

Question

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.

1
9
6/15/2015 8:59:33 PM

Popular Answer

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.

4
2/21/2017 12:59:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow