Using Entity Framework with MySQL – a Code First example

This post describes the very basic actions needed to setup a code first approach with Entity Framework (EF) and MySQL Database. For the sake of simplicity a console app is used. The purpose of this post is not to explain Entity Framework or Code First, it is just a getting started guide.

Open Visual Studio and create a new console application:

In Visual Studio, go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution…

Select “browse” and enter “Entity Framework MySQL” in the search box.

Select “MySQL.Data.Entityframework”

At the right side, check “Project” and click install.

Next, open the App.config file. Add a section directly below the <startup> section:

<add name="DefaultConnection" providerName="MySql.Data.MySqlClient" connectionString="server=[your mySql server address];User Id=[db user]; Persist Security Info=True;database=[database name];password=[your password]" />

In the entityFramework section delete all providers that do not have invariantname=”MySql.Data.MySqlClient”. There should be only one provider.

Your App.config file should resemble this (with your own server, db,name and credentials):

Next, you will need to write a table class and the DBContext class. To keep things simple we will place this class in program.cs.

Open program.cs, add references to system.Data.Entity and MySQL.Data.EntityFramework. On top of the code:

using System.Data.Entity;
using MySql.Data.EntityFramework;

Now add an Author Class , directly under the opening { of the namespace:

public class Author
public int Id { get; set; }
public string Name { get; set; }

This simple class will represent the Authors table in the database. As this is a code first approach, this table does not yet exist!

Following thing to do is to add a DBContext class. Write this directly below the Author class:

public class MyDbContext : DbContext
    public DbSet<Author> Authors { get; set; }
    // Constructor - set name of connection
    public MyDbContext() : base("name=DefaultConnection")

Three things to note here:

  • The attribute DbConfigurationType. Needed by Entity Framework to know it is a MySQL Database to talk to
  • The public DBSet. This tells Entity Framework there will be a table called Authors, of which the model is the class Author.
  • The (empty) constructor, needed to point to the right connection in App.config.

Next, open the Package Manager Console (Tools>NuGet Package Manager>Package Manager Console)

You will need to tell your project that you will use migrations. This is a one time action. In the Package Manager Console, type: enable-migrations.

If everything is ok, you will get the following message:

If you get errors, check the connection string.

Last step is to generate the Authors table in the database. To do this, first make a migration, after that update the database.

Again, in the package Manager Console, type add-migration [name for the migration]:

The result:

Finally, update the database:


Done! We have used the Code First approach in Entity Framework to generate a table in a MySQL database.

Leave a Reply