I recently determined that there are no significant performance gains from using a Dependency Injected DbContext in .NET Core and using async await calls as opposed to creating a new DbContext every time I want to access the DB.
But now I need to know why.
I did a much more granular test with System.Diagnostics.Stopwatch in my .NET Core 1.1 API services (which the controller is calling) in which I ran the stopwatch only when accessing the DB. The results were surprising.
When using the standard Dependency Injected context and async/await calls:
var task1 = _InjectedDbContext.Table1.FirstOrDefaultAsync(p => p.Id == SomeId);
var task2 = _InjectedDbContext.Table2.Where(u => u.AnotherId == SomeOtherId).ToListAsync();
(var result1, var result2) = await (task1, task2).WhenAll();
each DbContext query took significantly less than 100 ms.
However, when using this method:
using (var context = new DbContext(_InjectedContextOptions.Options))
{
var task1 = context.Table1.FirstOrDefaultAsync(p => p.Id == SomeId);
var task2 = context.Table2.Where(u => u.AnotherId == SomeOtherId).ToListAsync();
(var result1, var result2) = await (task1, task2).WhenAll();
}
Each DbContext query took anywhere from 100-230 ms.
FYI, here is my code for the DI setup in Startup.cs ConfigureServices:
var connection = Configuration.GetConnectionString("mydb");
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connection));
And here is my code for providing the DbContextOptions as a singleton whenever I create a new DbContext:
var dbContextOptions = new DbContextOptionsBuilder<MyDbContext>();
dbContextOptions.UseSqlServer(Configuration.GetConnectionString("MyDb"));
services.AddSingleton(dbContextOptions);
I also determined that the lag is not caused by simply the creation of the DbContext in the using statement (which is a very fast operation). What is going on here? Is it trying to re-connect to the DB every time or something?