If you think why Code First migrations are so important, even if we have inbuilt Database Initializers? You can read section Understanding Database Initializers which all have minor or major disadvantages. Migrations play important role when the data model changes and existing database records both are important for us. Migration can modify your existing database schema without deleting any existing records. There are two types of migrations:
- Automatic
- Code base or Manual.
In this article we are discussing Code base or Manual Migrations.
Enabling Migrations
To enable Migrations in your project go to Tools, Library Package Manager, then Package Manager Console
Note: Select project where DataContext file located.
type below command,
PM>Enable-Migrations
When we fire Enable-Migrations command it will automatically check DataContext class is corresponding to any existing database or not. This command will add "Migrations" folder with Configuration class and InitialCreate migration class in the project.
Note: You can set automatic migration through writing command
Enable-Migrations -EnableAutomaticMigrations,
Configuration class
You can add the configuration level setting in constructor so it will automatically initialize when configuration class instance generate. AutomaticMigrationsEnabled= false will prevent from automatic migration every time when the application runs. you can add another property AutomaticMigrationDataLossAllowed = false, this will prevent existing data loss. Seed method used to insert default values in datatable. EF by default write comment code for sample instruction.
InitialCreate migration class
Each migration class has two methods Up() and Down(). You can update Up and Down methods for any changes. When we run migrations, Up method will call by default and update the database based on code written in its scope. Down method exactly opposite of Up method. Down method will use when we want to revert our migrations. In our example Up method is creating tables Department and Student whether in Down method, it is dropping all tables and removing all constraints.
Enable Migration command will automatically add new table __MigrationsHistory in the database. MigrationId is unique and exactly the same name as your migration name in application. ContextKey will indicate appropriate Context Model.
Add migrations
Please read our previous article [^], We used the same example to perform migration operations. Now add Country field in Student.cs class and add a new migration in the project,
Write the following command in Package Manager Console to add migration,
PM> Add migrations AddCountryToStudent
It will create new migration class in your Migrations folder.
- using System.Data.Entity.Migrations;
- namespace MVCEFCodeFirstApp.Migrations
- {
- public partial class AddCountryToStudent: DbMigration
- {
- public override void Up()
- {
- AddColumn("dbo.Student", "Country", c => c.String());
- }
- public override void Down()
- {
- DropColumn("dbo.Student", "Country");
- }
- }
- }
Update Database
When we are adding new migration don't forgot to update database for sync. To update the database write the following code in Package Manager Console. -verbose is used when you want to get SQL query.
PM>Update-Database -Verbose
After execute update-database command it will sync and update database.
Original article
link.
Reference links