简体   繁体   中英

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.

In SQL it would look like this

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)

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).

There are a few EF extension packages, however, that claim to support this batch update and batch delete scenario. 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. 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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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