You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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().
The text was updated successfully, but these errors were encountered:
@divega , when I move my init (seed) code to Program, when I attempt to start debugging my browser is opened and then immediately closed. Any idea why this happens? Is this another "undocumented feature" ?
I believe I figured it out. In your example, you show capturing the instance of the host like so: var host = BuildWebHost(args);, but you fail to include the line of code that tells the host to run at the end like so: host.Run();
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:
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()
intoProgram.Main()
.The text was updated successfully, but these errors were encountered: