I'm currently trying to accomplish bulk inserts/updates with EF, since I have a lot of data incoming that needs to be saved into the Db. At every 1000 insert or update commands I dispose of the context in order to keep the number of entities it tracks low-ish.
I achieve this by calling the dispose method of my unit of work right after SaveChanges()
:
public void Dispose()
{
context.Dispose();
context = null;
}
Then instantiating a new context and giving it to the unit of work to use from then on.
The prcess gets repeated every 60 seconds and so far it seems to be working ok-ish, but from time to time I can see that the SQL server gets stuck between the 60 second cylces. A Query that fetches all data that's currently in db is used before the inserts in order to find out if I only need to update certain entities. (Thus I only update entities that I already have in DB)
var _allMatchDbEntries = matchRepository.GetAll()
.Include(x => x.Bets)
.ToList();
This EF Select statement hangs for 30-40 seconds (untill what I'm guessing is a lock getting removed) and then fires and completes in under 1 second. (I tested the query separately in management studio as well it isn't slowing at all)
I have no idea why this delay/lockup would occur. Could anyone tell me if I need to do anything else besides calling Context.Dispose()
in order to help SQL server get unstuck? Or is this standard behaviour of calling SaveChanges()
?
p.s. while this select query using join was hanging, simple ones like select * from matches table and select * from bets table were working just fine in MangementStudio.