EC Core Lazy Database Connection

Viewed 82

I'm using EF Core with .NET Core 5 and have a database connection that is dependency injected into my different controllers. Here's how the database context is created via ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDbContext>(opt =>
    {
        opt.UseMySql(Settings.Instance.SQLConnectionString, ServerVersion.AutoDetect(Settings.Instance.SQLConnectionString), b =>
        {
            b.UseNewtonsoftJson();
        })
    });
}

This works great, but I've realized this code runs for every API method that gets called, even those that don't need a database context. For example, this controller has an empty constructor and no database context usage, but still calls UseMySql:

[Produces("application/json")]
[Route("client")]
public class SimpleClientController : Controller
{
    [HttpPost("GetTime")]
    public IActionResult GetTime([FromBody] GetTimeRequest request)
    {
        // return the current UTC server time
        return Json(new GetTimeResponse()
        {
            Time = DateTime.UtcNow
        });
    }
}

In general this isn't a big deal, but some issues were highlighted during the recent us-east-2 AWS outage, which showed that methods that do not rely on the database were blocked by the lack of database connection. My redis/dynamodb methods do not suffer the same fate if there is a redis or dynamodb outage, as they are handled via a singleton service and only used lazily by methods that require them.

Is there a way to do something similar with EF Core and the database context? Ideally we only initialize EF/the database if the controller has to use the context.

1 Answers

Looks like my issue was actually with the automatic version detection, which is creating a connection to MySQL every single time to detect the version. I've now cached the version and it seems to have fixed the issue.

Related