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:
Now, to enable migrations and create the the data base we need to fallow this steps:
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
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
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
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"
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: