bernos
12/12/2013 - 7:27 AM

MySql with entity framework 5.x

MySql with entity framework 5.x

<configuration>
    <system.data>
        <DbProviderFactories>
            <remove invariant="MySql.Data.MySqlClient"/>
            <add name="MySQL Data Provider" 
                 invariant="MySql.Data.MySqlClient" 
                 description="Data Provider for MySQL" 
                 type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
        </DbProviderFactories>
    </system.data>
    <connectionStrings>
        <add name="DataContext" 
             connectionString="server=localhost;database=MyDatabase;User Id=root;Password=" 
             providerName="MySql.Data.MySqlClient" />
    </connectionStrings>
</configuration>
We're assuming MySql.Data.Entities v6.7.4 for this guide.

First install MySql.Data.Entities:

Install-Package MySql.Data.Entities

Then install EF version 5.0.0:

Install-Package EntityFramework -Version 5.0.0
using MySql.Data.Entity;

namespace MyProject.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<MySqlEntityFrameworkTest.ChinookContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;

            // IMPORTANT! We need to specify our code generator and sql generator, or we'll
            // get unusable migration classes and sql scripts
            CodeGenerator = new MySqlMigrationCodeGenerator();
            SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
        }

        protected override void Seed(MySqlEntityFrameworkTest.ChinookContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}