In Focus

Code First Migration - ASP.NET MVC 5 With Entity Framework And MySQL

In this article, we are going to explain Code First Migration in ASP.NET MVC 5 with EntityFrameWork & MySql.

Introduction
 
We know how to use Code First Migration in SQL Server. But in most cases, a customer will think we can use it for the open source database. So that’s the reason we pick the “MySQL” database, and we can follow the same steps we follow in the “SQL” database. In this article, we are going to explain Code First Migration in ASP.NET MVC 5 with Entity FrameWork & MySQL.
 
Prerequisites
  1. MySQL Installer
  2. MySQL Workbench
  3. Visual Studio ( We are using Visual Studio 2017 Community Edition ).
Create a Web Application using MVC 5
 
Click on File -> New -> Project -> Visual C# -> Web -> ASP.Net Web Application ( .Net Framework ).
 
 
 
Click on “OK” then click on “MVC”.
 
Install Entity Framework & MySQL Entity
 
Go to Visual Studio “Tools -> NuGet Package Manager -> Manage Nuget Packages for Solution” or right click on your web application then click on “Manage NuGet Packages”.
 
EntityFramework
 
Search EntityFramework in the “Browse” Section.
 
 
MySql.Data.Entity
 
Search MySql.Data.Entity in the “Browse” Section.
 
 
Once we installed EntityFramework & MySql Entity in our application then it will generate a SQL and MySQL Provider inside the EntityFramework Section in Web.Config.
  1. <entityFramework>  
  2.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />  
  3.     <providers>  
  4.       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />  
  5.       <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers>    
  6.   </entityFramework>  
Model Class
 
We just created a sample model class for demo purposes.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.    
  6. namespace WebAppWithMySql.Models  
  7. {  
  8.     public class Student  
  9.     {  
  10.         public int Id { getset; }  
  11.    
  12.         public string Name { getset; }  
  13.    
  14.         public string Password { getset; }  
  15.     }  
  16. }  
Creation of DBContext
 
Create a db context class in our application. The following dbcontext will point out our connection string in WebConfig.
  1. using MySql.Data.Entity;  
  2. using System.Data.Entity;  
  3. using WebAppWithMySql.Models;  
  4.    
  5. namespace WebAppWithMySql  
  6. {  
  7.     [DbConfigurationType(typeof(MySqlEFConfiguration))]  
  8.     public class WebAppContext : DbContext  
  9.     {  
  10.         public DbSet<Student> Products  
  11.         {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public WebAppContext()  
  16.             //Reference the name of your connection string ( WebAppCon )  
  17.             : base("WebAppCon") { }  
  18.     }  
  19. }  
Connection String
 
We added the same connection string name that we added in the dbcontext class. The following connection string represents “MySql” Db.
  1. <connectionStrings>  
  2.     <add name="WebAppCon" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;userid=root;password=rajeesh123;database=WebAppMySql;persistsecurityinfo=True" />  
  3.   </connectionStrings>  
Migration Steps
 
Go to Visual Studio "Tools -> NuGet Package Manager -> Package Manager Console". Then execute the following command. 
  1. Enable-Migrations – ( We need to enable the migration, only then can we do the EF Code First Migration ).
  2. Add-Migration IntialDb (migration name) – ( Add a migration name and run the command ).
  3. Update-Database -Verbose — if it is successful then we can see this message (Running Seed method).
Once Migration is done; then, we can see that the respective files are auto-generated under the “Migrations” folder.
 
 
Output
 
 
 
Reference
See Also
 
You can download other ASP.NET Core source codes from MSDN Code using the link mentioned below.
Summary
 
In this article, we are going to explain Code First Migration in ASP.NET MVC 5 with Entity Framework & MySQL. I hope this article is useful for all Azure beginners.