Consider when Entry() is used on a collection of objects:
c#
void findEntriesNotYetInDb(List<Blog> blogs)
{
var blogsNotYetInDb = new List<Blog>();
foreach(var blog in blogs)
{
if (dbContext.Entry(blog).State == EntityState.Detached)
blogsNotYetInDb.Add(blog);
}
return blogsNotYetInDb;
}
The method Entry() can end up taking up a significant portion of the CPU time, and end up hanging the UI for the duration of the loop. It would be nice to have a DbContext.EntryAsync() variant to use instead.
@vslee EF has async methods to handle asynchronous I/O. We can't really support compute-bound async in any meaningful way. This is a case where the application should off-load the processing itself to ensure the U.I. doesn't get blocked. Also, remember that every operation on the context must finish before a new operation can start, so make sure to block access to the context from other parts of the U.I. while it is being used like this.
Got it, thank you for the guidance.
Most helpful comment
@vslee EF has async methods to handle asynchronous I/O. We can't really support compute-bound async in any meaningful way. This is a case where the application should off-load the processing itself to ensure the U.I. doesn't get blocked. Also, remember that every operation on the context must finish before a new operation can start, so make sure to block access to the context from other parts of the U.I. while it is being used like this.