Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating 1.x to 2.0 should cover what to do with database initialization code in Startup.Configure #4134

Closed
divega opened this issue Aug 28, 2017 · 3 comments · Fixed by #4253
Assignees
Labels
Pri1 High priority, do before Pri2 and Pri3
Milestone

Comments

@divega
Copy link
Contributor

divega commented Aug 28, 2017

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().

@scottaddie scottaddie self-assigned this Aug 28, 2017
@divega
Copy link
Contributor Author

divega commented Aug 28, 2017

@clockwiseq
Copy link

@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" ?

@clockwiseq
Copy link

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pri1 High priority, do before Pri2 and Pri3
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants