Description
Having logic inside Startup.Configure()
that will take care of making sure the database is created, its schema updated, and that it is seeded with data isn't necessarily a good practice and in some cases it is known to be a bad idea.
That said, sometimes there aren't much better choices, it seems to have become a very common practice (possibly copied from some of our samples), and now it is causing customers pain when moving from ASP.NET Core 1.x to ASP.NET Core 2.0.
E,g., quoted from an internal email thread:
In ASP.NET Core 1.x using EF Core 1.x, a command such as dotnet ef migrations add instantiates a Startup instance, calls ConfigureServices() to add all services to the DI container including DbContext types, and performs its tasks.
In ASP.NET Core 2.0 using EF Core 2.0, the same is true but after ConfigureServices(), it also calls Configure(). The problem here is that a lot of code I’ve seen, including mine, calls database initialization code from Configure(). This creates a chicken and egg problem: If the database doesn’t exist yet, the seeding code will run before the actual command execution kicks in. Even worse, this will make an innocent dotnet ef migrations list fail if the database doesn’t exist yet.
For lack of a better pattern for discovering services and configuration at design time in ASP.NET Core applications, it seems that the safest recommendation we can provide is to move any logic that isn't about configuring the request pipeline from Startup.Configure()
into Program.Main()
.