Share via


ASP.NET MVC 5: Code First Migration With Entity Framework And 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 and 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 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 your web application and click “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 and MySql Entity in our application it will generate a SQL and MySQL Provider inside the EntityFramework Section in Web.Config.

<entityFramework>  
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />  
    <providers>  
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />  
      <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>    
  </entityFramework>

Model Class

We just created a sample model class for demo purposes.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
    
namespace WebAppWithMySql.Models  
{  
    public class  Student  
    {  
        public int  Id { get; set; }  
    
        public string  Name { get; set; }  
    
        public string  Password { get; set; }  
    }  
}

Creation of DBContext

Create a dbcontext class in our application. The following dbcontext will point out our connection string in WebConfig.

using MySql.Data.Entity;  
using System.Data.Entity;  
using WebAppWithMySql.Models;  
    
namespace WebAppWithMySql  
{  
    [DbConfigurationType(typeof(MySqlEFConfiguration))]  
    public class  WebAppContext : DbContext  
    {  
        public DbSet<Student> Products  
        {  
            get;  
            set;  
        }  
        public WebAppContext()  
            //Reference the name of your connection string ( WebAppCon )  
            : base("WebAppCon") { }  
    }  
}

Connection String

We added the same connection string name that we added in the dbcontext class. The following connection string represents “MySql” Db.

<connectionStrings>  
    <add name="WebAppCon" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;userid=root;password=;database=WebAppMySql;persistsecurityinfo=True" />  
  </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, we can see that the respective files are auto-generated under the “Migrations” folder.

Output

Reference

Summary

In this article, we explained Code First Migration in ASP.NET MVC 5 with Entity Framework and MySQL. Hope this article is useful for all Azure beginners.

See Also

It's recommended to read more articles related to ASP.NET Core & Azure App Service.