Entity Framework 6 (EF6) provided the option to manage the data model in a visual format (via EDMX) or by a code first approach (purely code classes). As the visual format is being phased out from the new Entity Framework 7 (EF7), here is a primer for starting a new project using the Code First approach. This primer assumes you are already familiar with the basics of Code First development and will focus on the use of the relatively new Code First Migrations feature.

Code First Migrations

Code First Migrations in Entity Framework are used to manage model changes. All changes to the model will generate a change script by the framework. Thus, provides the flexibility and robustness of interchanging the database at any particular time.

Getting started

The migrations feature needs to be enabled before it can be used in a Code First solution. To enable migrations in a Visual Studio project:

  1. By default, Automatic Migrations is enabled. To disable, edit the ?configuration.cs? file and set ?AutomaticMigrationsEnabled = false?.
  2. With automatic migrations, changes to the model will automatically apply to the database. Therefore, in a team environment, it is difficult to manage and resolve changes; especially merging change conflicts. Further reading from Microsoft can be found here.

Creating a change set

Once changes have been made to the models class, a change set is created ("abc" is used as a reference name).

  1. Run the ?Add-Migration abc? command in the package manager to create a change set.
  2. Run the ?Update-Database? command in the package manager to apply the changes to the database.
  3. You can rollback to a previous database schema by using the
    ?update-database -TargetMigration:?abc??command.
  4. If you are working in a team environment, you may need to use the
    ?Add-Migration 'abc' -IgnoreChanges? command in package manager. This is required when the model is being updated at the same time by multiple sources. Further reading from Microsoft can be found here.

Database Initialization options

There are several options available when initializing a database from the model. The following is a recommended approach based on our experience:

  1. Development Environment
    1. Setup the database initializer to drop and recreate the database when schema changes occur.
    2. Use the Seed() method of the initializer to populate lookup tables and for test data.
  2. Production Environment
    1. Turn off the initializer (e.g. you would not want the Seed() method making changes to your data or your database being dropped on schema changes).
    2. Further information can be found here.

Viewing a visual representation of the model

To view a visual representation of the model, use the Entity Framework Power Tools add-in. This presents the model to you as a read-only EDMX so that it cannot be accidently modified. Further information can be found here.

Sample Project

A good source of information (especially for newbies) about the new EF7 can be found here.