Entity framework update one column by increasing the current value by one without select(实体框架通过在不选择的情况下将当前值增加一来更新一列)
问题描述
我想要实现的是简单的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 });
希望对你有所帮助.
这篇关于实体框架通过在不选择的情况下将当前值增加一来更新一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实体框架通过在不选择的情况下将当前值增加一来更新一列


基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30