Introduction
This guide explains how to configure MySQL database with Visual Studio using Entity Framework Code First approach. The process involves installing MySQL, configuring Visual Studio, setting up required NuGet packages, and implementing a working example.
MySQL Installation
Begin by downloading the MySQL installer from the official website. Choose the appropriate version based on your network connectivity:
- If you have an active internet connection during installation, download mysql-installer-web-community
- If you're offline, download mysql-installer-community
During the custom installation setup, select the following components:
- MySQL Server - The main database engine
- MySQL for Visual Studio - Provides integration tools for VS
- MySQL Workbench - Visual client for database management
Additionally, install the MySQL Connectors if you plan to use ADO.NET-style connectivity. After installation completes, proceed with the configuration wizard.
Configuring Visual Studio for MySQL
Prerequisites
After installing MySQL for Visual Studio, the new item templates will include Entity Framework options. Without this installation, these templates won't appear in the Add New Item dialog.
Installing Required Packages
Entity Framework requires specific NuGet packages to communicate with MySQL. Install the following package via NuGet Package Manager:
- MySql.Data.Entity.EF6
This package automatically includes the necessary DLL references for Entity Framework operations with MySQL.
Application Configuration
The default configuraton uses SQL Server, so modifications are required to enable MySQL support. Update your App.config or Web.config with the following settings:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
<connectionStrings>
<add name="SampleDbContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=SampleDbContext;user id=root;password=secret123;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>
Update the version numbers according to your installed MySQL Connector version.
Practical Implementation
Create a new Console Application project and add an empty Code First model using the ADO.NET Entity Data Model wizard.
Database Context Definition
namespace DbConnector
{
using System;
using System.Data.Entity;
using System.Linq;
public class SampleDbContext : DbContext
{
public SampleDbContext()
: base("name=SampleDbContext")
{
}
public virtual DbSet<Account> Accounts { get; set; }
}
}
Entity Model Definition
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DbConnector
{
public class Account
{
[Key]
public int AccountId { get; set; }
public string Username { get; set; }
}
}
Database Operations
static void Main(string[] args)
{
using (var context = new SampleDbContext())
{
Account newAccount = new Account() { Username = "Administrator" };
context.Accounts.Add(newAccount);
context.SaveChanges();
Console.WriteLine("Record saved successfully");
}
Console.ReadLine();
}
Note: Place Console.ReadLine() outside the using block to prevent the console window from closing immediately after execution.
Upon running the application, Entity Framework will automatically create a database named SampleDbContext in MySQL. You can verify this by inspecting the MySQL server using MySQL Workbench or command-line tools.