Try
using(var context = new Context())
{
context.Entry(new Entity { Id = 5} ).State = Deleted;
context.SaveChanges();
}
Question
I am processing a very large amount of data. In an effort to speed up EF, I've found that calling SaveChanges()
in chunks has helped. Also, I read that resetting the context every so often will speed things up as well. I am having trouble implementing this.
context = new Context();
List<object> results = context.someQuery.ToList();
// do stuff to results to get it ready for processing in the for loop
for (int i = 0; i < results.Count; i++)
{
// do stuff like Add and Modify
if (results[i] == target)
context.myDataSet.DeleteObject(target);
if (i % 10 == 0)
context.SaveChanges();
if (i % 100 == 0)
{
context.Dispose();
context = new Context();
}
}
It seems like DeleteObject()
is not working once the context is reset. What can I do to get something along these lines working? I really don't want to have to re query the db (i.e., move the query into the for loop and do it again every time I reset the context), since I do a lot of preprocessing to it and don't want to keep repeating this.
Solution
Try
using(var context = new Context())
{
context.Entry(new Entity { Id = 5} ).State = Deleted;
context.SaveChanges();
}
OTHER TIPS
I think you can handle this using transactions (or Unit of Work pattern as a better solution):
context = new Context();
var transaction = context.Database.BeginTransaction();
List<object> results = context.someQuery.ToList();
// do stuff to results to get it ready for processing in the for loop
for (int i = 0; i < results.Count; i++)
{
// do stuff like Add and Modify
if (results[i] == target)
context.myDataSet.DeleteObject(target);
if (i % 10 == 0)
context.SaveChanges();
if (i % 100 == 0)
{
transaction.Commit();
transaction.BeginTransaction();
}
}
Do you have to rollback in case of an error in your queries? If so, please check out Unit of Work pattern