Code First Migration In Entity Framework - Part Five

In the previous article, I have explained how to implement a code first approach with a CRUD operation when we don’t have an existing database. In this article, I will explain about Code First Migrations and how enable Code First Migrations.

In the previous article, I have explained how to implement a code first approach with a CRUD operation when we don’t have an existing database. In this article, I will explain about Code-First Migrations and how to enable Code-First migration. Let’s see step by step.

See the previous articles for basic details.

Step 1

Before starting the further discussions we will open the previous project, i.e., Entity Framework in MVC - Part Four.

Open Visual Studio >> File >>Open Project >> Choose the previous project or you can download Entity Framework in MVC - Part Four.

After that, we will go to the Employee class in the Models folder.

  1. [Table("TblEmployee")]  
  2.     public class Employee  
  3.     {  
  4.         [Key]  
  5.         public int EmpId { get; set; }  
  6.         public string Name { get; set; }  
  7.         public string Address { get; set; }  
  8.         public string Email { get; set; }  
  9.         public string MobileNo { get; set; }  
  10.     }  

In the above code in our previous example, I did not give a string size. So, in the below code, I am giving the string size like Max Length and Min Length.

  1. [Table("TblEmployee")]  
  2.     public class Employee  
  3.     {  
  4.         [Key]  
  5.         public int EmpId { get; set; }  
  6.         [Required()]  
  7.         [StringLength(100,MinimumLength =4)]  
  8.         public string Name { get; set; }  
  9.         [Required()]  
  10.         [StringLength(200,MinimumLength =10)]  
  11.   
  12.         public string Address { get; set; }  
  13.         [Required()]  
  14.         [StringLength(200,MinimumLength =10)]  
  15.   
  16.   
  17.         public string Email { get; set; }  
  18.         [Required()]  
  19.         [StringLength(10,MinimumLength =10)]  
  20.   
  21.         public string MobileNo { get; set; }  
  22.     }  

Now, we will run the project. After that, it will give an error like in the below image.

Entity Framework

Step 2

Now, we will resolve this problem. In the above problem, we have to Enable Migrations.

Enable Code-First Migrations

The Migrations feature enables us to change the data model and deploy our changes to production by updating the database schema without having to drop and recreate the database.

So for this, go to Tools >> NuGet Package Manager >> Package Manager Console.

After that, type Enable-Migrations on command and press Enter. After that, we will get an output like this.

Entity Framework

In the above error, what happened is that in our application, there are two data contexts.

  1. EmpDataContext
  2. ApplicationDbContext(This is default generated)

So, here is the ambiguity which the Dbcontext executes. So, we have to give specific DbContext class.

Entity Framework

Press Enter and see the result. Then again, there comes some error like below.

Entity Framework

And, now, if we have already Migration configurations, then go to Solution Explorer and search Migrations folder and delete.

Entity Framework

And also, set -EnableAutomaticMigrations

Automatic Migrations allow us to use Code-First Migrations without having a code file in our project for each change we have done. Not all changes can be applied automatically - for example, the column renames require the use of a code-based migration. For more details, you can go through this link.

Now, again, give the full command.

Enable-Migrations -ContextTypeName Example3.Models.EmpDataContext –EnableAutomaticMigrations

Now, press Enter.

Entity Framework

Now, we have got the output successfully.

Step 3

Now, we have to update the database so again, Type on Command.

Update-Database –Script - Force

And press Enter.

Entity Framework

Now, go to the Migrations folder and open the configurations class and set as below code.

  1. public Configuration()  
  2.         {            
  3.             ContextKey = "Example3.Models.EmpDataContext";  
  4.             AutomaticMigrationsEnabled = true;  
  5.             AutomaticMigrationDataLossAllowed = true;  
  6.         }  

And, again write the below command and press Enter.

Entity Framework

After clicking on Execute symbol, one window will appear and then we have to give SQL credentials. Then, we will click on the Connect button, like below image.

Entity Framework

After that, let us check our database in SQL.

Entity Framework

Summary

Finally, we know how to perform the Code-First approach if the database does not exist.