Chapter 9 EF6 Support

MySQL Connector/Net 6.8 integrates support for Entity Framework 6.0 (EF6), but also offers support for Entity Framework 5 (EF 5). This chapter explains the new features in Entity Framework 6 implemented in MySQL Connector/Net 6.8.

Requirements for Entity Framework 6.0 Support


Configure Connector/Net to support EF6 by the following steps:

  1. An important first step is editing the configuration sections in the App.config file to add the connection string and the MySQL Connector/Net provider for EF6:

        <add name="MyContext" providerName="MySql.Data.MySqlClient"
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
            <provider invariantName="MySql.Data.MySqlClient"
                type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
            <provider invariantName="System.Data.SqlClient"
                type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
  2. Add the reference for MySql.Data.Entity.EF6 assembly into the project. Depending on the .NET Framework used, the assembly is to be taken from either the v4.0 or the v4.5 folder).

  3. Set the new DbConfiguration class for MySql. This step is optional but highly recommended, since it adds all the dependency resolvers for MySql classes. This can be done in three ways:

    • Adding the DbConfigurationTypeAttribute on the context class:

    • Calling DbConfiguration.SetConfiguration(new MySqlEFConfiguration()) at the application startup.

    • Set the DbConfiguration type in the configuration file:

      <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

    It is also possible to create a custom DbConfiguration class and add the dependency resolvers needed.

EF6 Features

Following are the new features in Entity Framework 6 implemented in MySQL Connector/Net 6.8:

Code First Features

Following are new Code First features supported by Connector/Net:

Example for Using EF6


using MySql.Data.Entity;
using System.Data.Common;
using System.Data.Entity;

namespace EF6
  // Code-Based Configuration and Dependency resolution
  public class Parking : DbContext
    public DbSet<Car> Cars { get; set; }

    public Parking()
      : base()


    // Constructor to use on a DbConnection that is already opened
    public Parking(DbConnection existingConnection, bool contextOwnsConnection)
      : base(existingConnection, contextOwnsConnection)


    protected override void OnModelCreating(DbModelBuilder modelBuilder)

  public class Car
    public int CarId { get; set; }

    public string Model { get; set; }

    public int Year { get; set; }

    public string Manufacturer { get; set; }


using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;

namespace EF6
  class Example
    public static void ExecuteExample()
      string connectionString = "server=localhost;port=3305;database=parking;uid=root;";

      using (MySqlConnection connection = new MySqlConnection(connectionString))
        // Create database if not exists
        using (Parking contextDB = new Parking(connection, false))

        MySqlTransaction transaction = connection.BeginTransaction();

          // DbConnection that is already opened
          using (Parking context = new Parking(connection, false))

            // Interception/SQL logging
            context.Database.Log = (string message) => { Console.WriteLine(message); };

            // Passing an existing transaction to the context

            // DbSet.AddRange
            List<Car> cars = new List<Car>();

            cars.Add(new Car { Manufacturer = "Nissan", Model = "370Z", Year = 2012 });
            cars.Add(new Car { Manufacturer = "Ford", Model = "Mustang", Year = 2013 });
            cars.Add(new Car { Manufacturer = "Chevrolet", Model = "Camaro", Year = 2012 });
            cars.Add(new Car { Manufacturer = "Dodge", Model = "Charger", Year = 2013 });