Jasiel Torres
Jasiel Torres

Reputation: 427

How to perform migrations to two databases, one for testing and one for production from a single project in EF?

I have a project in blazor wasm, in the appsettings.json file I have two connection strings, one points to the development database and the other to the production database, the production database is a copy of the database of development data.

appsettings. json file:

 "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqlConnectionDesarrollo": "Data Source=10.0.0.41;Initial Catalog=QaInventarios;User ID=Innovacion;Password=***;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
    "SqlConnectionProduccion": "Data Source=10.0.0.41;Initial Catalog=Inventarios;User ID=Innovacion;Password=***;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
  },

startup file:

services.AddDbContext<ApplicationDbContext>
                (options => options.UseSqlServer(Configuration.GetConnectionString("SqlConnectionDesarrollo")));  

the problem is that to perform a migration what I do is comment a connection string depending on the database I want to use and changing the name in the startup, I know it's a bad practice but I don't know how to optimize this, then I execute:

Add-Migration NameMigration
Update-Database

to perform a migration, in the development database it works fine, a new migration is created and the changes are made, then I run the Update-Database command again to update the production database and the changes are not made and are not added the new migration and the changes are not made, the same thing happens if I delete any migration with Remove-Migration -Force and then an Update-Database

there are inconsistencies in my database, how can I perform migrations, changes in both and that they remain exactly the same ??

Upvotes: 0

Views: 613

Answers (1)

Anuraj
Anuraj

Reputation: 19618

In EF Core 5.0, a connection string option is introduced in the EF Core CLI tool.

Example

For Development

dotnet ef database update --connection <Development Connection string>

And for Production.

dotnet ef database update --connection <Production Connection string>

Documentation about dotnet ef update

Upvotes: 1

Related Questions