I am still in the process of getting to know all of the ins and outs of Entity Framework, and I currently find it helpful on a couple of bulk updates to use Entity Framework's ExecuteSqlCommand directly, as it is significantly faster when processing many many thousands of updates (especially when I only need to update a single field). My question is two-fold: first, is this a safe way to handle my updates? To my knowledge, the standard way of updating through EF would be something like
if (user != null)
{
_Context.Entry(user).CurrentValues.SetValues(updatedUser);
_Context.SaveChanges();
}
or
{
_Context.Entry(updatedUser).State = EntityState.Modified;
_Context.SaveChanges();
}
but both of these methods bog down if the number of users to update exceeds ~5000. Am I missing something, or is my use of ExecuteSqlCommand acceptable?
Second, if it is safe to rely on ExecuteSqlCommand, can I hold on to a context whose sole purpose is this task, rather than re-creating it each time I need it? My understanding is that in general, contexts should never be maintained as they will slow down over time with use, but I'm wondering if this holds true for such a limited-use scenario as this as well.
If my fundamental approach to EF's use is in error, I would appreciate someone pointing that out as well. Thanks!