What is Code First Migrations in MVC and What is Procedure to do Code First Migrations? [Resolved]

Posted by Kumarkrishna184 under ASP.NET MVC on 12/13/2015 | Points: 10 | Views : 452 | Status : [Member] | Replies : 4
What is Code First Migrations in MVC and What is Procedure to do Code First Migrations?

Thanks and Regards,
Krishna Kumar



Responses

Posted by: Rajnilari2015 on: 12/13/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
What is Code First Migrations?

Code First Migration helps to create/update or change the data model that are created for the application using POCO objects.We need not to re-create or drop the database.This can be done by using Package Manager Console from VS environment.

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/13/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
What is Procedure to do Code First Migrations?

Step 1:

Create the POCO class objects

public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int EmployeeAge { get; set; }
}

public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public int EmpID { get; set; }

public virtual Employee Employee { get; set; }
}


Step 2:

Mapping Details

public class DataContext : DbContext
{
public DataContext():base("DefaultConnection"){ }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().HasKey(e => e.EmployeeID);
modelBuilder.Entity<Employee>().Property(p => p.EmployeeName).HasColumnType("VARCHAR").IsRequired().HasMaxLength(50);
modelBuilder.Entity<Employee>().Property(p => p.EmployeeAge).HasColumnType("INT").IsRequired();

modelBuilder.Entity<Department>().HasKey(d => d.DepartmentID);
modelBuilder.Entity<Department>().Property(p => p.DepartmentName).HasColumnType("VARCHAR").IsRequired().HasMaxLength(50);

modelBuilder.Entity<Department>().HasRequired(d => d.Department).WithMany().HasForeignKey(e => e.EmpID);

}
public DbSet<Department> Department { get; set; }
public DbSet<Employee> Employee { get; set; }
}


Step 3:

From VS open Package Manager Console,
Tools -> Library Package Manager -> Package Manager Console


Step 4:

For creating New Database, enable the below from PM

PM> Enable-Migrations
PM> Add-Migration <MigrationsName>
PM> Update-Database

hope this helps

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kumarkrishna184 on: 12/14/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks Niladri Sir.......

Thanks and Regards,
Krishna Kumar

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/14/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Kumarkrishna Sir, you r welcome Sir

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response