8

Building a data access layer for an internal enterprise MVC application with Entity Framework and the repository pattern. In some cases, I need to update, say, 100k rows in a table with 100 million or so rows in a SQL Server 2008 R2 database.

I am using EF6, targeting an existing legacy database we are refactoring. I am new to Entity Framework, but have much experience with SQL Server.

No matter how I seem to structure my update statement, when I run a profiler, I see 60k individual updates by ID. This can take up to 5 minutes to run. However, I have say a "batch number" that is indexed for a bunch of these records. Is there any way to UPDATE these records with a single where clause generated from EF? My solution has been to just write a simple sp that I then have EF call.

3
  • 1
    EntityFramework does not perform batched operations. Each operation will be one operation on the database per record (as you have discovered) You can either write and execute your own SQL statement through EntityFramework or call a stored procedure and pass the appropriate parameters through EntityFramework. Commented Dec 4, 2015 at 18:40
  • In my experience I would go with a SP for such 'large' operations. Commented Dec 4, 2015 at 18:40
  • 1
    The current EF v6 versions don't support batching up operations - but the EF team is hard at work on EF v7 and it will eventually support batch operations. For now - you either need to resort to your own solutions, or have a look at third-party solutions like EntityFramework.Extended Commented Dec 4, 2015 at 19:06

1 Answer 1

4

Entity Framework in its current version does not support bulk operations in the sense that you are wanting it to. Each update or delete will be one individual transaction per record. To avoid this, you can run SQL directly from Entity Framework if you do not want to go through the hassle of creating a stored procedure (though, they are pretty easy to get up and going)

using (var context = new MyDbContext())
{
    context.ExecuteStoreCommand("Update MyTable Set Value = {0} Where SomeColumn = {1}", 
                                updateValue, queryValue);
    context.ExecuteStoreCommand("Delete From MyTable Where value = {0}", 
                                myValueToQueryBy);
}

Note the use of the parameter by using syntax that looks very similar to a string.Format(). Please see the MSDN for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. My instincts make me shy away from SQL text in my .cs files, but I understand the concept. I'm basically making it our practice going forward when we use entity framework to always utilize a stored procedure when updating multiple rows on the known "big" tables. I have a generic data repository pattern, and for "TableA", I add a new method to call the stored procedure to the "TableARepository" class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.