MikaelEliasson / EntityFramework.Utilities

Provides extensions for EntityFramework that doesn't exist out of the box like delete and update by query and bulk inserts
443 stars 175 forks source link

How do you update an entity with an in-memory collection? would it be able to support that? Thanks, Hung! #36

Closed hunguyen75 closed 9 years ago

MikaelEliasson commented 9 years ago

EFUtilities can't do this out of the box and there is no real support in SQL-Server that I know of. What you can do is to use the batch insert against a temporary table and then run a sql script to update the real table from that one.

MikaelEliasson commented 9 years ago

@hunguyen75

From 1.0.0 EFUtilities can do this. See the documentation at:

https://github.com/MikaelEliasson/EntityFramework.Utilities#batch-update-entities

Example:

var commentsFromDb = db.Comments.AsNoTracking().ToList();
var rand = new Random();
foreach (var item in commentsFromDb)
{
    item.Reads = rand.Next(0, 9999999);
}
EFBatchOperation.For(db, db.Comments).UpdateAll(commentsFromDb, x => x.ColumnsToUpdate(c => c.Reads));