Is there any way to know if DbContext.OnConfiguring()
is being called from the Package Manager?
Here's my method.
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (string.IsNullOrWhiteSpace(DataPath))
throw new InvalidOperationException("No database path is specified.");
// Configure SQLite
options.UseSqlite($"Data Source={DataPath}");
}
This code works fine in my WinForms application. However, when adding migrations via the Package Manager Console, DataPath
will be null and an exception is thrown.
When run from Package Manager Console, it's okay if DataPath
is null, as no actual database is used in that case. But I still want to throw an exception if DataPath
is null while my application is running.
Is there any way to detect when this code is called from my application, and when it's called from Package Manager Console?
Note that I'm using .NET 6 and EF 6.
Note
Looking at the current entry assembly is one answer to this question. However, because that would run every time an instance of my DbContext
class is created, it would not be ideal from a performance perspective. In addition, I am not sure if the name of the DLLs could change in future versions of .NET. The ideal solution would be a more direct way, either as a property of the DbContextOptionsBuilder
instance that is passed to the method, or by setting my own flag by detecting difference in the way the class is instantiated.