Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework migration strategy for multiple instances

I have a .NET core app which I'am running on AWS Elastic Container Services (ECS). - The app runs on two different instances. - Database is SQL server

The app runs the database migrations on startup, which has worked really well. But then i had to migrate a lot of data which meant that the migration took longer time. This resulted in duplicates of the data being moved.

This happens because both apps first checks the database if the migration has been executed, both finds out that it hasn't, then both starts running the migration which takes time. After it is done it adds the migration to the database.

How do people solve this?

Possible solutions I and others have thought of

  1. Start with only one instance of the app, then scale up. this would work, but then I will have to manually scale down and up for each time there is a migration. (It is possible to do it automatically, but it would take time)

  2. Wrap long running migrations in transactions and at the start set the migration as done in the database. Check if it is in the database before commiting the change. If the transaction fails, remove the migration from the database.

  3. Lock the database? EF Core lock the database during migration . Seems weird.

  4. Make the migration a part of the deployment process. This seems to be best practice, but it would mean that the Build server would need to know the Database secrets. I'am not to afraid to give it, but it would mean i would have to maintain a duplicate set.

What does people out there do? Am I missing some obvious solution?

Thanks

like image 888
Moddaman Avatar asked Feb 13 '19 12:02

Moddaman


People also ask

How do you add migration to multiple contexts?

Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.

How do I turn on automatic migration in Entity Framework?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations –EnableAutomaticMigration:$true command (make sure that the default project is the project where your context class is).

How does migration work in Entity Framework?

Migration is a way to keep the database schema in sync with the EF Core model by preserving data. As per the above figure, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model.


1 Answers

We also used to have our applications perform the migration, but even Microsoft recommends avoiding this in a multi-instance environment:

We recommend production apps should not call Database.Migrate at application startup. Migrate shouldn't be called from an app in server farm. For example, if the app has been cloud deployed with scale-out (multiple instances of the app are running).

Database migration should be done as part of deployment, and in a controlled way.

Like everything there are different ways to go about solving the problem. Our team is small and so we generate migration scripts through the EF CLI tooling and then run them manually as part of a deployment/maintenance routine. This could of course be automated if your process warrants it.

like image 166
Justin Helgerson Avatar answered Dec 01 '22 16:12

Justin Helgerson