asp.net-corepublishing

ASP.NET Core MVC : once I have published a project, how do I force migrations to update to the database?


I have a project I have been working on in ASP.NET Core MVC, and now that I have published it:

enter image description here

I want to be able to force all migrations onto the database active on our server (my local database is just a copy so naming and table names and etc are the same).

Once I copy across the files to the server folder and run it, currently none of the migrations update the database and I get missing columns and tables and etc errors.

I have tried using cmd on the published file with

dotnet AppName.dll database update

as many people have suggested, but this didn't work at all. It doesn't seem to read the command at all.

enter image description here

Can someone please give me one straight answer? I have been struggling to find an answer that doesn't explain what to do or has old no longer working methods.


Solution

  • You can use

    app.UseDatabaseErrorPage();
    

    in your StartUp.Configure. This will display the Apply Migrations Page you know from Developing.

    Another way would be to create a migration script in development and use it against the production database. You can create an idempotent script with cmd while being in the Project Directory (where the StartUp.cs is located).

    dotnet ef migrations script --output "migration.sql" --idempotent
    

    This will create a script (a new file migration.sql) that will take any database that is on a prior level in the migration history to the current level.

    In code you could use dbContext.Database.Migrate();