简体   繁体   English

使用Entity Framework更新记录而不先加载

[英]Update records with Entity Framework without loading in first

I'm looking for a way to write code with Entity Framework to update 1000's of records that match a criteria. 我正在寻找一种使用Entity Framework编写代码的方法来更新符合条件的1000条记录。

In SQL it would look like this 在SQL中它看起来像这样

UPDATE [Items] 
SET [IsInSeason] = 1 
WHERE [Name] like '%summer%'

It doesn't make sense for me to load in all items into memory just for this update. 对于我来说,只为此更新将所有项目加载到内存中是没有意义的。

And I would like to avoid writing regular SQL statements (if possible) 我想避免编写常规的SQL语句(如果可能的话)

Out of the box, Entity Framework has no such ability. 开箱即用,实体框架没有这种能力。 You either need to load the entities before updating, or you need to resort to raw SQL (as an ad-hoc statement, or by calling a stored procedure). 您需要在更新之前加载实体,或者需要使用原始SQL(作为临时语句或通过调用存储过程)。

There are a few EF extension packages, however, that claim to support this batch update and batch delete scenario. 但是,有一些EF扩展包声称支持此批量更新和批量删除方案。 I haven't had any personal experience with any of them, but give them a look: 我没有任何个人经验,但请看一看:

To work with Entity Framework I believe you have to load the data into memory. 要使用Entity Framework,我相信您必须将数据加载到内存中。 How else are you going to give your statements? 你怎么还要发表你的陈述? You could write a stored procedure that you call from your data layer if you really don't want to load the data into memory and just let SQL Server handle it (if you're working with SQL Server) 如果你真的不想将数据加载到内存中并让SQL Server处理它(如果你正在使用SQL Server),你可以编写一个从数据层调用的存储过程

You can get all items and update them in loop like this 您可以获取所有项目并像这样循环更新它们

(from x in dataBase.Items
         where x.Name.Contains("summer")
         select x).ToList().ForEach(xx => x.IsInSeason=1);

if you use entity framework ,you should get all items as objects , do the nessecar changes , and save them . 如果您使用实体框架,您应该将所有项目作为对象,进行nessecar更改并保存它们。

暂无
暂无

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

相关问题 实体框架3.5 - 如何在不首先从数据库加载行的情况下更新行 - Entity Framework 3.5 - how to update row without first loading it from database Entity Framework Core 3.0 - 无需先删除或加载然后插入即可更新实体导航属性 - Entity Framework Core 3.0 - Update an entities navigation property without first deleting or loading and then inserting C# 实体框架:仅更新第一批记录并停止 - C# Entity Framework: Update Only First Batch Records and Stop .RemoveRange无需先在实体框架中获取数据库记录 - .RemoveRange Without fetching database records first in Entity Framework 使用Entity Framework Core 1.0更新记录,而无需更新/更新范围 - Updating records with Entity Framework Core 1.0 without needing to Update/UpdateRange 没有Include(“ A”)方法的实体框架代码第一次延迟加载 - Entity Framework Code First Lazy Loading without Include(“A”) method 实体框架 6 多对多更新,无需更新或加载子项 - Entity Framework 6 Update Many to Many without updating or loading Child 实体框架核心更新数据库 - 无需迁移的代码优先方法 - Entity Framework Core Update Database - Code First Approach Without Migration 实体框架First()不返回任何记录 - Entity Framework First() does not return any records 如何确定是否在未加载相关记录的情况下设置实体框架中的导航属性 - How to determine if Navigation Property in the Entity Framework is set without loading the related records
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM