Code First Migrations In Entity Framework Step By Step

Here are the steps:
  
Model Classes: Suppose you have the following two entities within your application.
  1. public class Employee  
  2. {  
  3.     [Key]  
  4.     public int Id  
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     [Required]  
  10.     public string Code  
  11.     {  
  12.         get;  
  13.         set;  
  14.     }  
  15.     [Required]  
  16.     public string Name  
  17.     {  
  18.         get;  
  19.         set;  
  20.     }  
  21.     [Required]  
  22.     public string OptionType  
  23.     {  
  24.         get;  
  25.         set;  
  26.     }  
  27.     public bool Active   
  28.     {  
  29.         get;  
  30.         set;  
  31.     }  
  32.     public string Notes  
  33.     {  
  34.         get;  
  35.         set;  
  36.     }  
  37.     public ICollection < OptionsHistory > OptionsHistories   
  38.     {  
  39.         get;  
  40.         set;  
  41.     }  
  42. }  
  43. public class OptionsHistory   
  44. {  
  45.     [Key]  
  46.     public int Id   
  47.     {  
  48.         get;  
  49.         set;  
  50.     }  
  51.     public string Code   
  52.     {  
  53.         get;  
  54.         set;  
  55.     }  
  56.     public string Name   
  57.     {  
  58.         get;  
  59.         set;  
  60.     }  
  61.     public string OptionType  
  62.     {  
  63.         get;  
  64.         set;  
  65.     }  
  66.     public bool Active   
  67.     {  
  68.         get;  
  69.         set;  
  70.     }  
  71.     public string Notes  
  72.     {  
  73.         get;  
  74.         set;  
  75.     }  
  76.     public int OptionId   
  77.     {  
  78.         get;  
  79.         set;  
  80.     }  
  81.     public string OptionsVersion  
  82.     {  
  83.         get;  
  84.         set;  
  85.     }  
  86.     public Options Options   
  87.     {  
  88.         get;  
  89.         set;  
  90.     }  
  91. }  
Here [Key] decorated column is a primary key column.
Mapping Details: DataContext class and Fluent API mapping details for above two entities are given below.
  1. public class OptionCodeContext: DbContext   
  2. {  
  3.     public OptionCodeContext(): base("name=DbConnectionString")  
  4.     {  
  5.   
  6.     }  
  7.     public DbSet < Options > options  
  8.     {  
  9.         get;  
  10.         set;  
  11.     }  
  12.     public DbSet < OptionsHistory > optionsHistories  
  13.     {  
  14.         get;  
  15.         set;  
  16.     }  
  17.   
  18.     protected override void OnModelCreating(DbModelBuilder modelBuilder)   
  19.     {  
  20.         modelBuilder.Entity < Options > ().HasKey(p => p.Id);  
  21.   
  22.         modelBuilder.Entity < Options > ().Property(c => c.Id)  
  23.             .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);  
  24.   
  25.         modelBuilder.Entity < OptionsHistory > ().HasKey(b => b.Id);  
  26.   
  27.         modelBuilder.Entity < OptionsHistory > ().Property(b => b.Id)  
  28.             .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);  
  29.   
  30.         modelBuilder.Entity < OptionsHistory > ().HasRequired(p => p.Options)  
  31.             .WithMany(b => b.OptionsHistories).HasForeignKey(b => b.OptionId);  
  32.   
  33.         base.OnModelCreating(modelBuilder);  
  34.     }  
  35. }  
Connection String: And, your database connection string is given below:
  1. <add name="DbConnectionString" connectionString="data source=SampleDataSource;initial catalog=OptionsCode;integrated security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />  
Visual Studio Package Manager Console: 

To create the database for these two entities within your application, go to the Package Manager Console option as shown below:

console

Creating New Database

Running Commands: Run the following command to configure migrations within your project and for creating new database.
  1. Enable migrations

     
    Enable-Migrations

  2. Create migration

    Add-Migration MigrationsName

  3. Create upgrade/downgrade script

    Update-Database
When you run above listed command on the Package Manager Console windows one by one then: Firstly, a Migrations folder will be added into your applications having Configuration.cs file for Migrations configuration setting.

solution
  1. Secondly, a class file will be created having name suffix as MigrationsName followed by underscore and a unique generated number. This file will have all the entities to be created in the database.

  2. Thirdly, a new database will be created having initial catalog name (initial catalog = YourDBName) as given in your connections string.
Suppose you have added one more class named as Customer into your data model classes

Now for updating the database with new changes run the following commands:
  1. Create migration
    Add-Migration MigrationsName

  2. Create upgrade/downgrade script
    Update-Database
When you run above listed command on the Package Manager Console windows one by one, then:
 
Firstly, a class file will be created having name suffix as MigrationsName followed by underscore and a unique generated number. This file will have all the entities to be created or updated in the database.

solution

Undo/Rollback a Migrations

You can also Undo/Rollback specific migrations by using the following commands:
  1. Rollback to a specific migrations
    Update-Database -TargetMigration:MigrationsName

  2. Rollback all migrations
    Update-Database -TargetMigration:0


Similar Articles