Advertise with us

How to use migration in code first entity framework?

Posted by  Sushant Mishra
 1037  View(s)
Rate this:

I am facing a problem while using code first approach in MVC. I have made the class of employee, context class and connection string. Still my database not generated. I don’t know how to use migration. See the code below:

Thanks in advance 

Employee class

public class Employee   
    public int ID { get; set; }
        public string EmpName { get; set; }
        public int Salary { get; set; } } 

Context class 

public class DepartmentDB_Context: DbContext
    public DepartmentDB_Context() : base("DepartmentDB_Context") { }
        public DbSet<Employee> EmployeeTable { get; set; } } 


    <add name="DepartmentDB_Context" connectionString="data source=.; User Id=sa; Password=1234; initial catalog=DepartmentDB_Context;" providerName="System.Data.SqlClient"/>

  1. Re: How to use migration in code first entity framework?

    You need to enable migration from package manager console and add migration

    First step is to enable migration to enable migration type following command in package console manager

    PM> enable-migrations

    Second step is to add migration use following command

    PM> add-migration v1

    Third step is to update database for updating migration use following command                                              

    PM> update-database v1

     I hope your problem will be solved after using these step

      Modified On Apr-11-2018 11:21:22 PM