![](/img/trans.png)
[英]How to update multiple records in a table Using the Linq for Entity Framework Core
[英]How to update all the records of a table in an entity framework?
我想更新表的所有记录并更改一列值,但它对我不起作用
我试过的:
public static void UpdateSellerAutoApprovalSettings(bool isSellerAutoApproved)
{
using (var context = GetDbContext())
{
context.SiteGroups
.AddOrUpdate(x => x.IsSellerAutoApproved,
new SiteGroup { IsSellerAutoApproved = isSellerAutoApproved });
context.SaveChanges();
}
}
只需使用ForEach
扩展方法:
context.SiteGroups.ForEach(e => e.IsSellerAutoApproved = isSellerAutoApproved);
请检查以下方法。
如果您需要有条件更新,您可以使用 SiteGroups 和 ForEach 之间的 where
public static void UpdateSellerAutoApprovalSettings(bool isSellerAutoApproved)
{
using (var context = GetDbContext())
{
context.SiteGroups
.ForEach( x => x.IsSellerAutoApproved = isSellerAutoApproved )
context.SaveChanges();
}
}
您可以使用EntityFrmaework.Plus库,如下所示
context.SiteGroups
.Update(x => new SiteGroup { IsSellerAutoApproved = isSellerAutoApproved });
这将生成一条 SQL 语句,并且您不会将所有记录加载到应用程序内存中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.