Code First Migration – ASP.NET MVC 5 With EntityFrameWork & MySql


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.


  1. MySQL Installer
  2. Download 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 EntityFramework & 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”.


Search EntityFramework in the “Browse” Section.


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.

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<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=, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers>

Model Class

We just created a sample model class for demo purpose.

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 db context 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
public class WebAppContext : DbContext
public DbSet<Student> Products
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.

<add name="WebAppCon" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;userid=root;password=rajeesh123;database=WebAppMySql;persistsecurityinfo=True" />

Migration Steps

  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.


See Also

You can download other ASP.NET Core source codes from MSDN Code, using the link, mentioned below.


In this article, we are going to explain Code First Migration in ASP.NET MVC 5 with EntityFrameWork & MySql. I hope this article is useful for all Azure beginners.

Uploading Large Files to IIS / ASP.NET

When we upload a large file in using file upload control we got a how to over come this ? The Default size of Asp.Net is 4MB.


In Web.Config put this code.

//In IIS 7

<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
<requestFiltering><requestLimits maxAllowedContentLength="3000000000" /></requestFiltering>

The below code maxRequestLength measured in kilobytes.Here is an example with a 1GB request length and a 10 minute request timeout:

<httpRuntime maxRequestLength="1048576" executionTimeout="600" />  

The below code maxAllowedContentLength measured in bytes. 

<requestLimits maxAllowedContentLength="3000000000" />  

How to set Cookieless ASP.NET in WebConfig

Cookie = “(S(fkcy42h0yhqrb3g0f0y4j0r5))”

//Put this below code in web.config

<sessionState mode=”InProc” cookieless=”false” timeout=”30″ />


Timeout expired and The timeout period elapsed prior to obtaining a connection from the pool.


//This may have occurred because all pooled connections were in use and max pool size was reached.

Set in Max Pool Size=500 or above;

Eg :
<add name=DbCon connectionString=”Data Source=.;Initial Catalog=ShaShib;Integrated Security=True;pooling=’true’; Max Pool Size=6000;/>


Setting defualt page as start page in web confg file in

<defaultDocument enabled="true"> <!-- this line enables default documents for a directory -->
<clear/> <!-- removes the existing default document list --> 
<add value="rajeeshmenoth.htm"/> <!-- foo.htm is now the default document --> 
<add value="rajeeshmenoth.php"/> <!-- 2nd default document in search order --> 
<add value="rajeeshmenoth.aspx"/> <!-- 3rd default document in search order --> 
<add value="rajeeshmenoth.cfm"/> <!-- 4th default document in search order -->
<modules runAllManagedModulesForAllRequests="true"/>

Videos not working in web server in

//Put This code in web config..

<mimeMap fileExtension=".mp4" mimeType="video/mp4" />



Validation of viewstate MAC failed in

 Put this code in Web config…!! validation key contain long security line its single line.below i split into 3 lines.



<pages validateRequest="false" enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false"></pages>


<machineKey validation="SHA1"




decryption="Auto" decryptionKey="A1B2C3D4E5F6F6E5D4C3B2A1A1B2C3D4E5F6F6E5D4C3B2A1"/>


%d bloggers like this: