Introduction


In this article, we are going to explain Code First Migration in ASP.NET Core MVC 6 with EntityFrameWork Core, using Command Line Interface ( CLI ).

Before reading this article, you must read the articles given below for ASP.NET Core knowledge.

Model Class


We are going to create a sample Code First Migration project in ASP.NET Core 1.0. Model class given below contains the properties of the user details in our Applications.

using System; 
    
namespace CodeFirstMigration.Models 
    public class UserDetails 
    
        public Guid Id { get; set; } 
    
        public string Name { get; set; } 
    
        public string EmailId { get; set; } 
    
        public string Company { get; set; } 
    
}

LocalDB Configuration in ASP.NET Core 1.0


In the previous versions, everything is handled by Web.Config but in ASP.NET Core, the connection string is written in appsettings.json file. By default, it will show as a LocalDB path and as per our requirement, we can change the connection string path.

In the way given below, we can create connection string in ASP.NET Core 1.0.


Image Credit : https://rajeeshmenoth.wordpress.com

The appsetting JSON file contains the LocalDB connection string details in our Application and we have given the database name as "UserDb". If you want to know more about JSON file configuration in ASP.NET Core, please check our previous article ASP.NET Core 1.0: Adding A Configuration Source File.

  "ConnectionStrings": { 
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=UserDb;Trusted_Connection=True;MultipleActiveResultSets=true" 
  
}

DbContext in ASP.NET Core 1.0


The code givden below contains the information about EntityFrameWork Core DbContext. We can add the LocalDB connection string details with the help of "UseSqlServer" Method.

using Microsoft.EntityFrameworkCore; 
using Microsoft.Extensions.Configuration; 
    
namespace CodeFirstMigration.Models 
    public class CodeDbContext : DbContext 
    
        private IConfigurationRoot _config; 
    
        public CodeDbContext(IConfigurationRoot config, DbContextOptions options) : base(options) 
        
            _config = config; 
        
    
        public DbSet<UserDetails> userDetails { get; set; } 
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        
            base.OnConfiguring(optionsBuilder); 
    
            optionsBuilder.UseSqlServer(_config["ConnectionStrings:DefaultConnection"]); 
        
    
}

Seed Data in EntityFrameWork Core


We are going to implement Code First Migrations in Entity Framework Core to seed the database with test data and manually we are entering the seed data information in the "CodeDbSeedData" class. The code given below contains the default values of User Details Table in our Application.

using System; 
using System.Linq; 
using System.Threading.Tasks; 
    
namespace CodeFirstMigration.Models 
    public class CodeDbSeedData 
    
        private CodeDbContext _context; 
    
        public CodeDbSeedData(CodeDbContext context) 
        
            _context = context; 
        
    
        public async Task SeedData() 
        
            if (!_context.userDetails.Any()) 
            
                var user = new UserDetails() 
                
                    Id = Guid.NewGuid(), 
                    Name = "RajeeshMenoth"
                    EmailId = "rajeeshmenoth@gmail.com"
                    Company = "HappiestMinds Technologies Pvt Ltd" 
                }; 
    
                _context.userDetails.Add(user); 
                await _context.SaveChangesAsync(); 
            
        
    
}

Code First Migration


The steps given below will explain the step by step Code First Migration in EntityFrameWork Core.

Setting Project Location


The command given below will help to change our current "C Drive" to "F Drive" because currently our Code First Migration project is saved into "F Drive".


Image Credit : https://rajeeshmenoth.wordpress.com

Dotnet ef-help command


The command given below will display for more information about dotnet ef command.

"dotnet ef –help"


Image Credit : https://rajeeshmenoth.wordpress.com

Starting With new migration

 
We are going to start with new EntityFrameWork migration, using the command given below.

"dotnet ef migrations add IntialDB"


Image Credit : https://rajeeshmenoth.wordpress.com

Update the database


The command given below will update the EntityFrameWork Core database in ASP.NET Core Application.

"dotnet ef database update"


Image Credit : https://rajeeshmenoth.wordpress.com

Output



Image Credit : https://rajeeshmenoth.wordpress.com

Conclusion


We learned about Code First Migration in ASP.NET Core MVC 6 with EntityFrameWork Core, using Command Line Interface ( CLI ) and I hope you liked this article. Please share your valuable suggestions and feedback.

Reference