Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What app.UseMigrationsEndPoint does in .NET Core Web Application Startup class

I created a new .NET Core Web Application from Visual Studio and I got this piece of code generated in startup class:

if (env.IsDevelopment())
{
    // ***
    app.UseMigrationsEndPoint();
    // ***
}

What actually does this line app.UseMigrationsEndPoint()? The official docs are not helpful at all:

Processes requests to execute migrations operations. The middleware will listen for requests made to DefaultPath.

What's the default path? What to POST to it? What will it actually execute? Does it have any optional parameters? Is it protected somehow?

like image 217
Miroslav Zadravec Avatar asked Dec 21 '20 07:12

Miroslav Zadravec


2 Answers

As you maybe already seen from the linked documentation page, it comes from EF Core. The exact code, that will be run, can be found on GitHub.

It checks if the used database has any pending migrations and will apply them if needed. That's it.

like image 126
Oliver Avatar answered Oct 19 '22 14:10

Oliver


This app.UseMigrationsEndPoint() is actually a very handy tool in development.

As we develop, we add entities to the db context, or modify ones we have. We run dotnet ef migrations add <NameOfMigration> as usual, and that would generate migration files. I personally would typically run dotnet ef database update when the migrations add command succeeds. However, with app.UseMigrationsEndPoint(), I don't have to manually run dotnet ef database update.

So, we try to build and run our app in development. The browser loads our app, but the database update did NOT yet happen as we might expect.

However, if we tried to access a page or an API call that needed to access a table that hasn't yet been updated, we get a special page - not an error page - but one that lists all the migrations that haven't yet been applied.

Surprisingly, there is a button marked "Apply Migrations" (or something like that). We would click that, then our database would be updated according to the migrations not yet applied. We would then reload the page, and the browser will then display the page as we expect. We can check the tables in SQL Server Object Explorer or other tool, and we will see those migrations reflected.

like image 11
Mickael Caruso Avatar answered Oct 19 '22 16:10

Mickael Caruso