Using MariaDB with Entity Framework

Christian Mark picture Christian Mark · Nov 25, 2013 · Viewed 34.4k times · Source

Recently, I read a news that MariaDB is a drop-off replacement for MySQL since MySQL has unfriendly pricing for clustered/enterprise version according to Google.

Now I can't find anything relevant about EF for MariaDB on Google so I'm hoping someone knows about it. Is it ok to use MySQL driver for this since it is 100% compatible? Any thoughts?

Update

I just found out that RedHat is also switching from MySQL to MariaDB for it's default database management system. So it is necessary for my current project to switch it to MariaDB.

Answer

Panagiotis Kanavos picture Panagiotis Kanavos · Dec 20, 2013

I was able to use MariaDB 10 with Entity Framework although it required a bit of work mainly because the MySQL tools are a bit buggy.

To work with MySQL/MariaDB in Visual Studio 2010/2012,you need to install MySQL for Visual Studio using MySQL Installer. I used the Web version as I only wanted to download the connectors and the extensions. Once you do this, you can add connections to MariaDB and create EF models.

This is not enough to run your code though. First you need to add the MySQL Connector using NuGet.

Unfortuanetly, MySQL for Visual Studio adds a reference to an older provider version (mentioned here) and can't load the newer version. To fix this, I added the following section in my app.config:

<system.data>
   <DbProviderFactories>
     <remove invariant="MySql.Data.MySqlClient"/>
     <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" 
           description=".Net Framework Data Provider for MySQL" 
           type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
   </DbProviderFactories>
</system.data>

This replaces the old reference with a new one. Note that I used

<remove invariant="MySql.Data.MySqlClient"/>

not

<remove name="MySql Data Provider"/>

in the remove element.

Currently, MySQL for Visual Studio isn't supported in Visual Studio 2013

UPDATE - 2017

Connector/.NET is essentially stagnant, with the same problems it had in 2013, eg no true asynchronous calls. The "async" calls are fake - they are run on separate threads, defeating the very purpose of using async. That alone makes it unsuitable for web applications, where one wants to server as many requests as possible using the minimum number of threads/CPU.

Never mind about .NET Core support.

That's why in the past few years people have built their own, truly asynchronous providers. Some of the more popular ones are:

  • MySqlConnector offers a truly asynchronous provider for .NET and .NET Core
  • Pomelo offers EF Core support on top of MySQLConnector

With about 100K NuGet downloads each, frequent versions and active maintenance.

They aren't "official", but definitely worth trying

Lockdown Update - April 2020

It seems MySqlConnector and Pomelo have really taken off.

Connector/.NET finally released a couple of versions after almost two years with the latest, 8.0.19, getting 233K downloads.

MySqlConnector on the other hand, got 496K downloads for version 0.61.0. Minor updates are frequent, with the latest, 0.63.2 coming 8 hours before this post. That's probably a bit too frequent, but far better than 2 years.

I haven't checked features or MySql 8 compatibility yet. If I had to chose though (which I will probably do for a project next week), I'd start with MySqlConnector.

I suspect Connector/.NET will be forced to offer far more frequent updates going on, to keep pace with .NET Core releases, but that's just speculation at this point.