Lapenkov Vladimir
Lapenkov Vladimir

Reputation: 3218

asp core ef migrations on production server

Can't perform migrations on production server. Command "dotnet ef database update" works on my computer but fails on production Steps i tried are: 1. Fill in checkbox execute code first migrations in Visual Studio before publish. 2. dotnet ef database update not working . I installed .NET SDK but it doesn't have libraries needed.

Any suggestions appeciated.

Upvotes: 19

Views: 17913

Answers (4)

Eric Milliot-Martinez
Eric Milliot-Martinez

Reputation: 4596

To avoid making a sql script and just applying my migrations directly to an azure SQL server with connection string in my appsettings.Production.json file, I did so with the following and it worked:

dotnet ef database update --project example-api.domain --startup-project example-api.api --context ExampleContext -- --environment Production

As a note, my dbContext.cs class resided inside of the example-api.domain project.

Upvotes: 0

Anders Carstensen
Anders Carstensen

Reputation: 4124

It is highly recommended you check which changes EF Core is going to perform using the dotnet ef migrations scripts command, as suggested by others.

However, once you are ready to perform your database migration, make sure your appsettings.Production.json file contains the necessary information to connect to your production database, and then run:

dotnet ef database update --context MyContext -- --environment Production

The -- token directs dotnet ef to treat everything that follows as an argument and not try to parse them as options. Any extra arguments not used by dotnet ef are forwarded to the app. (Source)

Upvotes: 3

Sajeeb Chandan Saha
Sajeeb Chandan Saha

Reputation: 865

You can generate a migration script by running the following command:

Script-Migration -From older_migration_name -To newer_migration_name -Context ApplicationDbContext

The script will have a random name and will reside in the following path:

<Your_Project_Drive>:\<Your_Project_Folder>\<Your_Project_Folder.Model>\obj\Debug\netcoreapp3.1\<Some_Random_Name>.sql

Now just run that script on the targeted DB of production server.

Upvotes: 11

bricelam
bricelam

Reputation: 30375

There are a couple options:

  1. Generate a SQL script using dotnet ef migrations script and run it on your production database.
  2. Call dbContext.Database.Migrate() at runtime during application startup (but be careful when running multiple apps/database clients)

Also, in the next release (1.0.0-preview3) of Microsoft.EntityFrameworkCore.Tools, we'll be shipping ef.exe which you can point directly to assemblies (instead of project.json files) to perform migrations.

Upvotes: 38

Related Questions