ASP.NET MVC 4

MVC: Database Migrations

The code first approach outlined here creates and re-creates a database from your C# Model classes. The down side is that each time the model is changed all the data is lost. To re-populate the database when it is recreated you need to add a ProjectInitializer class to your models folder. Click on the link and save the text file as DbInitialiser.cs. Make sure you change the name of the project in the namespace to match your project name.

In order to use this class, you need to modify your web.config to use the DbInitialiser (in this example called SchoolInitializer in the DAL folder) when your application starts

Seeding

Please make sure that your project name is used in place of ContosoUniversity, and the name of the class is not important you can spell it the british way DbInitialiser if you wish.

  • Contoso University: Part 5 this example shows two ways of seeding the database using data migrations or drop and re-create the database on change.

MVC: Enable Migrations

In the code first approach the database is created automatically from the pre-defined classes in the Models folder, this includes the membership database used to store user names and passwords based on IdentityUser. However when the models changes it is necessary to change the database to match

In MVC5 this can be achieved by enabling database migrations. Open the Package Manager Console window using the Tools menu. Enter "enable-migrations" at the PM> command prompt. This will create a Migrations folder with a Configuration.cs class

Configuration.cs

In the slide above the project has two database context classes, and in order to enable-migrations, the -ContextTypeName and the folder name (-MigrationsDirectory) need to be specified as well. A real example is shown below for enabling migrations on the ApplicationDbContext that is used for the users database that controls security and logins

Example of Identity migrations

The following command shows enabling migrations for the main database which is this case is about Qualifications.

Example of Qualifications migrations

In both cases the databases use the same connection string in the web.config file, so that the tables from both will end up in the same database. In the Migrations folders there is a configuration.cs file that can be used to seed the database with data as shown in the Seeding page.

Example of seeding

Add-Migration

Whenever the models are changed the database needs to be updated. If AutomaticMigrationsEnabled = false then you need to add a migration whenever the model is changed. If this is set to true then the migrations are automatically added whenever the system detects that the database and the models are not synchronised.

Adding a migration

The added migration InitialCreateproduces a C# class based on DbMigration to create the tables as defined by the model classes.

Example of Initial Create Migration

Update Database

The final stage is to update-database which executes all the migrations that are required in order to bring the database up to date compared to the models. It also runs any seed method that might be needed to add or update the seed data.

Update Database Initial Create

The database should now exist, and be up to date with the existing models. If you do not need to update the microsoft Identity database as well as the project database, the commands are a lot simpler as the package manager does not need to be told which database to update

  • enable-migrations
  • add-migration "InitialCreate"
  • update-database
  • add-migration "NextChange"
  • update-database

When using the package manager console, up-arrow will bring back the last entered command, and then you can copy parts or all of that command to make entering complicated commands a lot easier.

MVC Index