Entity Framework Add your first migration


Example

After you've enabled migrations (please refer to this example) you are now able to create your first migration containing an initial creation of all database tables, indexes and connections.

A migration can be created by using the command

Add-Migration <migration-name>

This command will create a new class containing two methods Up and Down that are used to apply and remove the migration.

Now apply the command based on the example above to create a migration called Initial:

PM> Add-Migration Initial
Scaffolding migration 'Initial'.
The Designer Code for this migration file includes a snapshot of your current Code
First model. This snapshot is used to calculate the changes to your model when you
scaffold the next migration. If you make additional changes to your model that you 
want to include in this migration, then you can re-scaffold it by running 
'Add-Migration Initial' again.

A new file timestamp_Initial.cs is created (only the important stuff is shown here):

public override void Up()
{
   CreateTable(
       "dbo.Authors",
        c => new
           {
               AuthorId = c.Int(nullable: false, identity: true),
               Name = c.String(maxLength: 128),
           })
        .PrimaryKey(t => t.AuthorId);
        
    CreateTable(
       "dbo.BlogPosts",
       c => new
           {
                Id = c.Int(nullable: false, identity: true),
                Title = c.String(nullable: false, maxLength: 128),
                Message = c.String(),
                Author_AuthorId = c.Int(),
            })
        .PrimaryKey(t => t.Id)
        .ForeignKey("dbo.Authors", t => t.Author_AuthorId)
        .Index(t => t.Author_AuthorId);
}
    
public override void Down()
{
    DropForeignKey("dbo.BlogPosts", "Author_AuthorId", "dbo.Authors");
    DropIndex("dbo.BlogPosts", new[] { "Author_AuthorId" });
    DropTable("dbo.BlogPosts");
    DropTable("dbo.Authors");
}

As you can see, in method Up() two tables Authors and BlogPosts are created and the fields are created accordingly. In addition, the relation between the two tables is created by adding the field Author_AuthorId. On the other side the method Down() tries to reverse the migration activities.

If you feel confident with your migration, you can apply the migration to the database by using the command:

Update-Database

All pending migrations (in this case the Initial-migration) are applied to the database and afterwards the seed method is applied (the appropriate example)

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target     
database.
Applying explicit migrations: [201609302203541_Initial].
Applying explicit migration: 201609302203541_Initial.
Running Seed method.

You can see the results of the activities in the SQL explorer: database overview

For the commands Add-Migration and Update-Database several options are available which can be used to tweak the activities. To see all options, please use

get-help Add-Migration

and

get-help Update-Database