How To use Migrations in MVC5

For a few times i have been building data bases using the code first approach, here i want to summarize the steps that i do to achieve that using an example.

A typical MVC5 project will include you classes and a user class called ApplicationDbContext that inherits form IdentityDbContext, this class holds the data witch a typical user will hold the user name, the password ,the user rolls and ect..

in this project, in addition to the user class i have an actor class with typical actor detail, i have created actor db class in a folder called DataContexts that sits in the root of the project that looks like this:

public class ActorsDb : DbContext
    {
        public ActorsDb()
            : base("DefaultConnection")
        {

        }

        public DbSet<Actor> Actors { get; set; }
    }

Now, to enable migrations and create the the data base we need to fallow this steps:

  1. In visual studio quick lunch type pack and in the suggested options choose package manager consol. After that a in the bottom a package manager consol window will appear. If you have multiple projects in the solution choose your project in the Default project drop down list
  2. in this window, after the PM>, type: enable-migrations
  3. Because in a typical project you will find more the one Db context (in my case: identity and Actors) you would have to choose witch migration to enable, or to enable both of them.
  4. In my case in got the massage:

    PM> enable-migrations
    More than one context type was found in the assembly 'GerysWebApi'.
    To enable migrations for 'GerysWebApi.DataContexts.ActorsDb', use Enable-Migrations -ContextTypeName GerysWebApi.DataContexts.ActorsDb.
    To enable migrations for 'GerysWebApi.Models.ApplicationDbContext', use Enable-Migrations -ContextTypeName GerysWebApi.Models.ApplicationDbContext.

    It is very important to set both migrations in a separate folders because both migrating can not be in the same folder

  5. First i have enabled the ApplicationDbContext migrations by typing:

    PM> enable-migrations -ContextTypeName ApplicationDbContext -MigrationsDirectory DataContexts\IdentityMigrations

    And i got this in return:

    Checking if the context targets an existing database...
    Code First Migrations enabled for project GerysWebApi.
    

    What happened is that Migration created a folder called DataContext and in it there is a configuration file and later code that will represent tables

  6. Enable the Actors Migrations by typing:

    PM> enable-migrations -ContextTypeName ActorsDb -MigrationsDirectory DataContexts\ActorsMigrations

    And now we got two enabled migrations, on for user and one for actors

  7. After we have have enabled a Migration we need to add a Migration to each of the enabled Migration folders, by adding a Migration i mean to add a class in c# that will represent our entity class (Actors) in a table like manor, each time we add a Migration we capture how our entity class is looking at the current time, we could return in time to past migration if we want to.

    To add a Migration to the actors class we need to take the name space of the configuration file in the ActorsMigrations folder that we created previously, my is - "GerysWebApi.DataContexts.ActorsMigrations" and incorporate it in a command like this -

    add-Migration -ConfigurationTypeName the configuration file namespace.Configuration the wanted name for the new file

    So in our case we use the command:

    PM> add-Migration -ConfigurationTypeName GerysWebApi.DataContexts.ActorsMigrations.Configuration "FirstCreate"

    To enable the identity use this command:

    PM> add-Migration -ConfigurationTypeName GerysWebApi.DataContexts.IdentityMigrations.Configuration "FirstCreate"
  8. Now, to really crate the the dada base we will use the Update command, then Migration will look for the latest added migration and create from it the real relational data base, you could see an mdf file would be created in the App_data folder with all the tables we wanted.

    We will update the two migrations by using the two commands:

    PM> update-database -ConfigurationTypeName GerysWebApi.DataContexts.ActorsMigrations.Configuration
    update-database -ConfigurationTypeName GerysWebApi.DataContexts.IdentityMigrations.Configuration

So now we have a data base with the wanted tables we wanted, we can change the data base by changing the entity and then repeat the steps of adding the migration and updating her

For more information you can visit these links: