Is there any way to detect if my application is being run through dotnet ef * or the PowerShell alternatives?
I'm creating a multi-tenant ASP.NET Core app with Entity Framework Core and I have logic to decide which connection string should be used, but should not be run if I'm creating migrations or updating a database through the command line. I cannot rely on checking for the existance of an HttpContext because I use my DbContext during configuration as well, nor can I use additional command line arguments or environment variables.
EDIT: Found a way to see if a connection string has already been configured, it's not ideal but I can use it to help with my particular use case:
optionsBuilder.Options.Extensions
.OfType<RelationalOptionsExtension>()
.FirstOrDefault()?.ConnectionString
If you create a type that implements IDbContextFactory<T> in your DbContext (or startup) project, the tools will call this instead of trying get your DbContext from services or instantiate it directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With