ASP.NET MVC 5 With Entity Framework And MySQL — Code First Migrations

<entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><parameters><parameter value="mssqllocaldb" /></parameters></defaultConnectionFactory><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.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers></entityFramework>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="DefaultConnection" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;userid=root;password=;database=testdatabase;persistsecurityinfo=True" /> </connectionStrings>
.
.
.
.
</configuration>
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MySQLCodeFirst.Models{    public class User    {        public int Id { get; set; }        public string FirstName { get; set; }        public string LastName { get; set; }    }}
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Web;namespace MySQLCodeFirst.Models{    public class DataContext : DbContext    {    }}
public DataContext()    : base("DefaultConnection") //This 'DefaultConnection' should be equal to the connection string name on Web.config.{    this.Configuration.ValidateOnSaveEnabled = false;}
using MySql.Data.Entity;namespace MySQLCodeFirst.Models{    [DbConfigurationType(typeof(MySqlEFConfiguration))]
public class DataContext : DbContext
{
.
.
.
.
}
public DbSet<User> Users { get; set; }
enable-migrations
add-migration UserTable
update-database

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nishān Wickramarathna

Nishān Wickramarathna

Systems Design • Social Innovation • Cloud • ML