Using MySql with Entity Framework

[A]fter spent time looking for various information on the internet finally i am able to combine Entity framework and MySql to work together, yeayy!. This blog post is for you who are having trouble in making Entity framework works with MySql.

EF MySql

I’m using Code First approach in this tutorial and also i create simple console application for the demo, follow below steps and you are ready to use Entity framework using MySql.

1. Install Entity Framework

Open Package Manager Console (Tools=> Library Package Manager => Package Manager Console), install entity framework for MySql.

1. Install EF MySql

2. Add connection string

Before add connection string make sure that you have created MySql database. On App.config add below setting as first child of configuration tag.

3. Create Model Class

Create model class that you will be using, for example i’m using Book.

4. Create Context Class

Next step is we define a context class that inherit DbCOntext.

5. Migration

If you have reached this step it does mean that you have finished setting up. Now, we will make sure that it is working by try to do migration.

Back to Package Manager Console, run this command

2. Enable Migration

This command will enable us to create migration. Take a look at Solution explorer, you will see that a folder and a class are added.

3. Migrations Folder

Next run below command. This command is used to create a migration

You will see in solution explorer that a file named 201502030435328_InitialCreate.cs is created.

Final step is run below command to update our database so that it has table for our model.

4. Update Database

Check your database, you should see that books table has been created for you.

5. Show Tables

6. Insert Record

By now you can use Entity framework to do CRUD operation.

6. Select From book

Hope you enjoy this tutorial 🙂

Facebook Comments

Agung Setiawan

Agung Setiawan adalah software engineer di, penulis sekaligus pecinta sastra, dan pembaca buku

Halo, perkenalkan saya Agung Setiawan.
Seorang Software Engineer di BukaLapak, penulis buku, blogger, gunpla builder, tech educator.
Simak pemikiran saya soal dunia Software Engineering via Twitter di @agungsetiawanmu dan facebook
Mau belajar Vim bareng saya?
Belajar ngoding dari nol menggunakan PHP

3 thoughts on “Using MySql with Entity Framework

Leave a Reply

Your email address will not be published. Required fields are marked *