Rico Suter's blog.
 


The following post describes the steps to setup and use Entity Framework Code First Migrations. For more information read this Microsoft page. The sample application shown in this article can be found on GitHub.

Initial setup

Listed below are the steps to setup a new project with an Entity Framework context, an entity class and an initial migration:

  1. Create a new data access project (C# library project) in your solution
  2. Open the Package Manager Console in the Visual Studio menu Tools > NuGet Package Manager > Package Manager Console. In the console window, select the previously created data access project as default project.
  3. Install Entity Framework as NuGet package:

    Install-Package EntityFramework         
    
  4. Implement a data context class which inherits from DbContext and the entity classes:

    public class DataContext : DbContext
    {
        public DbSet<Person> Persons { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            ...
        }
    }
        
    public class Person 
    {
        ...
    }
    
  5. Enable migrations in the Package Manager Console:

    Enable-Migrations 
    
  6. Create an initial migration:

    Add-Migration InitialCreate
    

    Note: “InitialCreate” is the default name for the first migration.

  7. Update the database with any pending migrations:

    Update-Database -Verbose
    

    If no database can be found, the command creates a new database in a LocalDB (a file-based Microsoft SQL instance). By default, you can connect to this database by using the connection string

    (localdb)\mssqllocaldb in the Microsoft SQL Server Management Studio. The instance name may be altered by changing the parameter value in the configuration file (App.config or Web.config):

    <entityFramework>
        <defaultConnectionFactory type="...LocalDbConnectionFactory, ...">
            <parameters>
                <parameter value="mssqllocaldb" />
            </parameters>
        </defaultConnectionFactory>
        ...
    </entityFramework>
    
  8. The command Update-Database created an App.config file in your data access project. If this project is not your startup project, you need to copy the Entity Framework configuration from the App.config file to the application configuration file (e.g. the Web.config file of your web project).

Create a new migration

To create a new migration after changing the entity model, just rerun the following command with an appropriate migration name:

Add-Migration MyMigration

Regenerate the database

To delete the existing database and completely regenerate the database and its tables, follow the following steps:

  1. Open Microsoft SQL Server Management Studio
  2. Connect to your SQL Server or LocalDB with the Microsoft SQL Server Management Studio
  3. Delete the database
  4. Optional: If you don’t need to migrate other databases, delete all migration classes from the “Migrations” directory of the data access project and recreate the first migration:

    Add-Migration InitialCreate
    
  5. Regenerate the database by running the following command:

    Update-Database -Verbose
    

Programmatically run migrations

If you’d like to automatically migrate the database after application deployment, just run the following code on application startup (e.g. in your Application_Start() method):

Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, 
    SampleEfMigrationsApplication.Data.Migrations.Configuration>());

This way the production database is always compatible with the deployed application.



Discussion