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.
- [Table("TblEmployee")]
- public class Employee
- {
- [Key]
- public int EmpId { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string Email { get; set; }
- public string MobileNo { get; set; }
- }
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.
- [Table("TblEmployee")]
- public class Employee
- {
- [Key]
- public int EmpId { get; set; }
- [Required()]
- [StringLength(100,MinimumLength =4)]
- public string Name { get; set; }
- [Required()]
- [StringLength(200,MinimumLength =10)]
-
- public string Address { get; set; }
- [Required()]
- [StringLength(200,MinimumLength =10)]
-
-
- public string Email { get; set; }
- [Required()]
- [StringLength(10,MinimumLength =10)]
-
- public string MobileNo { get; set; }
- }
Now, we will run the project. After that, it will give an error like in the below image.
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.
In the above error, what happened is that in our application, there are two data contexts.
- EmpDataContext
- ApplicationDbContext(This is default generated)
So, here is the ambiguity which the Dbcontext executes. So, we have to give specific DbContext class.
Press Enter and see the result. Then again, there comes some error like below.
And, now, if we have already Migration configurations, then go to Solution Explorer and search Migrations folder and delete.
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.
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.
Now, go to the Migrations folder and open the configurations class and set as below code.
- public Configuration()
- {
- ContextKey = "Example3.Models.EmpDataContext";
- AutomaticMigrationsEnabled = true;
- AutomaticMigrationDataLossAllowed = true;
- }
And, again write the below command and press Enter.
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.
After that, let us check our database in SQL.
Summary
Finally, we know how to perform the Code-First approach if the database does not exist.