Updating the Database with Code First

Updating the database with Code First is quite difficult. We need to use Migration for this. In our project we have created, we first open the Package Manager Console. Here we type Enable-Migrations and press the enter key. I said that because I enabled it earlier in this project. After you enable it, we will create a new folder named Migrations. Changes you make in this folder will be included.

If you want to create a new table and add it to the database, you can do as follows. With Migrations you can update your data again without ever having to delete it again.

After that, we need to enter the Package Manager Console again and enter the Update-Database command in order to save this change. When we enter we create a new table without deleting the data.

If you want to delete a column in a table or add a new column, we need to enter the command “Add-Migration Add”. Of course, after this process we need to update the database again. We can do this with the Update-Database command.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.