Chapter 10 EF 6 Support

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

Requirements for Entity Framework 6.0 Support

Configurations

Configure Connector/Net to support EF 6 by the following steps:

EF 6 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 EF 6

Model:

using MySql.Data.Entity;
using System.Data.Common;
using System.Data.Entity;
 
namespace EF6
{
  // Code-Based Configuration and Dependency resolution
  [DbConfigurationType(typeof(MySqlEFConfiguration))]
  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)
    {
      base.OnModelCreating(modelBuilder);
      modelBuilder.Entity<Car>().MapToStoredProcedures();
    }
  }
 
  public class Car
  {
    public int CarId { get; set; }
 
    public string Model { get; set; }
 
    public int Year { get; set; }
 
    public string Manufacturer { get; set; }
  }
}

Program:

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))
        {
          contextDB.Database.CreateIfNotExists();
        }
 
        connection.Open();
        MySqlTransaction transaction = connection.BeginTransaction();
 
        try
        {
          // 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
            context.Database.UseTransaction(transaction);
 
            // 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 });
 
            context.Cars.AddRange(cars);
 
            context.SaveChanges();
          }
 
          transaction.Commit();
        }
        catch
        {
          transaction.Rollback();
          throw;
        }
      }
    }
  }
}