Required Tools: Following two tools are required for Code First Migration
- Nuget,
- Entity Framework: Install or update using nuget “Install-Package EntityFramework”
Database Migration in asp.net enables us to update the database at runtime. Entity framework 6 has the power to manage multiple dbContext at a time. You can use the below pattern to implement the multiple datacontext for migration.
Steps to Migrate a database:
- Create Modal
- Create DataContext which must be inherited from DbContext
- Use nuget console manager
- “Enable-Migrations” in nuget console manager.
Eg: Enable-migrations -ContextTypeName [Name of the DataContext with Namespace] -MigrationsDirectory:“Name of Directory Where Migration files took place”
Parameters: Enable-migrations, -ContextTypeName, -MigrationsDirectory: - Add-Migration –configuration [Name of the Configuration file with Namespace, which was created by Enable-Migration, placed into the specified Migration Directory] “Specify the name of migration *Do not use Only numeric values for file names.”
Parameters: Add-Migration, –configuration - Update-Database -configuration [Name of the Configuration file with Namespace, which was created by Enable-Migration, placed into the specified Migration Directory] –Verbose
Parameters: Update-Database, –configuration, –Verbose
Note: Use –Verbose only if you wish to generate the sql script into nuget Console manager.
- “Enable-Migrations” in nuget console manager.
WORKING EXAMPLE
Step 1: Create Modal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace DemoProject.Models { public class Order { public int OrderID { set; get; } public int Quantity { set; get; } public double Amount { set; get; } public DateTime Date { set; get; } } } namespace DemoProject.Models { public class User { public int UserID { set; get; } public string FirstName { set; get; } public string LastName { set; get; } } } |
Step 2: Create DataContext which must be inherited from DbContext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
namespace DemoProject.Models { public class DataContext : DbContext { public DataContext() : base("DefaultConnection") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //modelBuilder.Entity<User>().Ignore(c => c.Age); } public DbSet<User> Users { get; set; } public DbSet<Order> Orders { get; set; } } } |
Step 3: Open nuget Console Manager
- “Enable-Migrations” with Qualified namespace of DataContext.
Enable-Migrations -ContextTypeName DemoProject.Models.DataContext -MigrationsDirectory “DemoDataContext”
Note: Above code generates a directory named “DemoDataContext” in then root location of the project and create a “Configuration.cs” file inside the directory. - Add-Migration –configuration [Name of the Configuration file with Namespace, which was created by Enable-Migration, placed into the specified Migration Directory] “Specify the name of migration *Do not use Only numeric values for file names.”
Add-Migration –configuration DemoProject.DemoDataContext.Configuration “Create”
Note: Above code generates a file named as [Something numeric value]_create.cs into “DemoDataContext” directory. - Update-Database -configuration [Name of the Configuration file with Namespace, which was created by Enable-Migration, placed into the specified Migration Directory] –Verbose
Update-Database -configuration DemoProject.DemoDataContext.Configuration –Verbose
Note: Above code generate the database into db, and the –Verbose keyword generate the sql code to insert into __Migration table into DB.