Home > Blockchain >  IDbContextFactory for Blazor: when to use CreateDbContextAsync the async version vs CreateDbContext
IDbContextFactory for Blazor: when to use CreateDbContextAsync the async version vs CreateDbContext

Time:02-20

I started using IDbContextFactory.CreateDbContext when working on Blazor server projects. I know this interface was created for Blazor to resolve the DbContext concurrency issue in a stateful Blazor server app.

So far I've always been doing

using var context = _contextFactory.CreateDbContext();
//Then use the context...

But I just found that there is an async version of the method, IDbContextFactory.CreateDbContextAsync which the documentation simply says "Creates a new DbContext instance in an async context".

So my question is should I prefer one version over another? Is the context creation an expensive/potentially blocking operation that warrants async operation?

CodePudding user response:

It serves no real purpose at present. DbContext initialization does not perform any IO or network access, and the extension points where you might do (DbContext constructor on OnConfiguring), are not themselves async.

In retrospect, it's possible we should not have added this

https://github.com/dotnet/efcore/issues/26630

  • Related