I'm setting up some Health Checks - specifically, some readiness checks for use with kubernetes. I want traffic to be routed to an instance only after it's gone through the EF Core "warmup" process.
I see the official EF Core health check calls context.Database.CanConnectAsync. I've extended this to at least cover that GetPendingMigrationsAsync returns an empty list, since if it doesn't, it can't really be considered ready.
I'd like to push this further to make sure it's all started up, i.e. the model is all loaded and the first query overhead is out of the way. What's the best way to do this? A call to context.Model.GetEntityTypes() or something? (This is a generic check at the moment, so I don't have access to any specific entity types or a solid context type)
@kierenj Some info that will hopefully help you decide what's appropriate in your case. Usually initialization falls into three broad categories:
DbContext instanceDbSet properties, but it's minimal and generally fast._ = context.Model;There are also some one-off initializations to build accessor delegates and the like, but these should not have a huge impact.
My intuition is that initializing the model to get most of the "global" stuff cached is probably what you should do.
Perfect, thank you. :)
To others coming here: In a pretty clean setup, assigning context.Model as a warmup measure reduces my first query from 1500ms to 600ms, subsequent queries are < 50ms (10k rows non-tracked)
Most helpful comment
@kierenj Some info that will hopefully help you decide what's appropriate in your case. Usually initialization falls into three broad categories:
DbContextinstanceDbSetproperties, but it's minimal and generally fast._ = context.Model;There are also some one-off initializations to build accessor delegates and the like, but these should not have a huge impact.
My intuition is that initializing the model to get most of the "global" stuff cached is probably what you should do.