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

EFBatchOperation.For error! #73

Open KeithBarrows opened 8 years ago

KeithBarrows commented 8 years ago

I just started playing with this Utility and it looks good so far. I am testing out some bulk updates and deletes on my SQL DB. I am batch updating a Set Tree Model (Celko Tree) and I need to update each node's Left and Right values for everything to the right of the target node. The commented out code (below) is the final result I am after. However, I am testing by setting another field so I can then go to SQL Mangler to see if the bulk update worked.

I am getting this error on the code below:

The variable name '@plinq0' has already been declared. Variable names must be unique within a query batch or stored procedure.

// Get all target node IDs, shift all remaining items left and delete target node(s)...
var targetList = _db.EntityTrees.Where(a => a.lft >= entity.lft && a.rgt <= entity.rgt).Select(a => a.Id).ToList();
var shiftLeft = targetList.Count * 2;
//EFBatchOperation.For(_db, _db.EntityTrees).Where(a => a.lft > entity.lft).Update(b => b.lft, b => b.lft - shiftLeft);
//EFBatchOperation.For(_db, _db.EntityTrees).Where(a => a.rgt > entity.rgt).Update(b => b.rgt, b => b.rgt - shiftLeft);
//EFBatchOperation.For(_db, _db.EntityTrees).Where(a => targetList.Contains(a.Id)).Delete();
EFBatchOperation.For(_db, _db.EntityTrees).Where(a => a.lft > entity.lft).Update(b => b.SystemId, b => shiftLeft);

_db.SaveChanges();

Since this table has over 700,000 records, I really need the bulk update! Any help is appreciated.

IDisposable commented 8 years ago

Any reason you're not doing all that work in a stored procedure? Seems like the best thing...

KeithBarrows commented 8 years ago

We are comparing both ways, Entity Framework vs SQL Sproc, seeing what the SQL Execution Plan looks like.