Entity Framework Migrations on an already installed system, how to run?

Asked

Viewed 459 times

1

A C# Winforms application, with the Entity Framework that accesses data in SQL Server in development phase we use Migrations to make the database according to our model, but then with the system already in production?

Usually on workstations and servers we do not have Visual Studio to run the "database update", before this, and also before my ignorance on the subject I ask how to run Migrations in the production environment?

We can use the code created by Migrations and add a functionality to the application, and this will maintain the database according to the model created for the application?

What I am doing is using the SQL script generated by Migrations to update the database, after a security backup to prevent data loss, but this would be the most appropriate procedure?

1 answer

0

If you use Migration:

So your first steps are:

Add-Migration primeiroExec
update-database

when Voce generates or changes some model then you will redo the same process:

Add-Migration minhaPrimeiraAlteracao
update-database

the add-migration will generate a file in the Migrations folder that will contain what will be changed in the database, and the update-database will run this file.

If you did something wrong just need to return to the previous Migration:

Update-Database primeiroExec

Generated Migration before time you can delete and then recreate:

Remove-Migration
  • Thank you for the @Hudsonph information... This is exactly what I do in the development environment, but in the production environment there is a way to do it without the visual studio as I mentioned in the question?

  • @Arivenuth Nao pq vc need the project to generate Migration in your case of code first, the best is what I recommend 'and not use Migration, and have your code free from Ef, in case you change for example to Dapper vc will not have problems.

  • I appreciate the tip to keep my EF code free, but at this stage of the project this is totally unfeasible. So the most indicated in my case, code first, is to create the initial migration generating the SQL script. If a change is needed, create the adaptation migration by generating the proper script and run it in the production SQL.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.