简体   繁体   中英

Entity Framework 6 large table bulk update

I'm new to Entity Framework so I hope this is not a nonsence.
Basically I have a large having between 30000 and 100000 records on it. Fundamentally, the interesting fields on each record are the ID and the Total which has to be calculated at application side.

So I'm fetching the records, project them to a Business Logic class performing the diverse calculations and assigning the new Total to that class.

var validPriceVersionID = context.PriceVersion.Where(f => f.Status == "VALID").FirstOrDefault().PriceVersionID;
                var tmp = context.EndItem.Where(f => f.Total == 0).Project().To<EndItem>().ToList();
                tmp.Select(c => { c.PriceVersionID = validPriceVersionID; return c; }).ToList();

Now, I need to update the whole SQL table with the new Total based on the ID and here is where my nightmare begins. For example, something as basic as below is taking an extraordinary amount of time

var idTotalPair = tmp.Select(x => new {x.EndItemID, x.Total}).ToArray(); 

I've made some researchs and found out that EF does not support bulk opperations and that it does not support TableTyped parameters for stored procedures.

So, what I pretend is to find best (performant) way to update all those records after each Total calculation. Any help will be greatelly appreciated :)

Thanks in advance

You can use a raw command. Something like:

   using (var context = new MyContext()) 
   { 
       context.Database.ExecuteSqlCommand( 
           "UPDATE EndItem SET Total = ... WHERE EndItemId = ..."); 
   }

The most efficient way is the @jeroenh comment. Making everything in a Stored Procedure instead if possible.

Disclaimer : I'm the owner of Entity Framework Extensions

This library is not free but allows you to perform Bulk Update and other operations for this kind of scenario:

  • Bulk SaveChanges
  • Bulk Insert
  • Bulk Delete
  • Bulk Update
  • Bulk Merge

Example

// Easy to use
context.BulkSaveChanges();

// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);

// Perform Bulk Operations
context.BulkDelete(endItems);
context.BulkInsert(endItems);
context.BulkUpdate(endItems);

// Customize Primary Key
context.BulkMerge(endItems, operation => {
   operation.ColumnPrimaryKeyExpression = 
        endItem => endItem.Code;
});

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