实体框架通过在不选择的情况下将当前值增加一来更新一列 [英] Entity framework update one column by increasing the current value by one without select

查看:7
本文介绍了实体框架通过在不选择的情况下将当前值增加一来更新一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是简单的sql查询:UPDATE TABLE SET COLUMN = COLUMN + 1

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?

编辑

我尝试了原始 sql 并且它有效.我必须从连接字符串中确定 sql 提供程序,并从连接上下文中确定数据库模式名称.之后我会使用对应的sql查询来更新表.

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.

对于 SQL,它看起来像 UPDATE schemaname.TABLE SET COLUMN = COLUMN + 1.对于 POSTGRESQL,我必须双引号架构名称、表名称和列名称:UPDATE "schemaname"."TABLE" SET "COLUMN" = "COLUMN" + 1.

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.

推荐答案

这里是解决方案.您可以使用以下代码:

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 });

希望对你有所帮助.

这篇关于实体框架通过在不选择的情况下将当前值增加一来更新一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆