简体   繁体   English

实体框架6如何在不重新加载的情况下基于旧值更新字段?

[英]Entity Framework 6 how to update field based on old value without reload?

I'm finding a way to implement the behavior below in sql: 我正在寻找一种方法来实现下面的sql行为:

update TableName set Column1 = Column1 + 2

Does new entity framework 6 supports this kind? 新的实体框架6是否支持这种?

Why not just use ExecuteSqlCommand 为什么不只使用ExecuteSqlCommand

context.Database.ExecuteSqlCommand(@"UPDATE TableName SET [Column1] = [Column1] + 2");

And ofcourse you may consider to make some wrapping method to make this generic if you'll use is in many tables 当然,如果要在许多表中使用,则可以考虑使用一些包装方法来使此通用

Not by default, but there is a NuGet package available that provides this feature. 默认情况下不是,但是有一个提供此功能的NuGet软件包可用。

https://github.com/loresoft/EntityFramework.Extended https://github.com/loresoft/EntityFramework.Extended

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM