实体框架通过在不选择的情况下将当前值增加一来更新一列

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

希望对你有所帮助.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)