Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Using MySQL

Trevor Pilley edited this page Mar 16, 2020 · 1 revision

Version Support

MicroLite is built to work with any version of MySQL from 5.5 onwards but requires MySql.Data to be installed as the .NET framework does not ship a native provider for MySQL.

Installing the Provider

Install the MySql.Data nuget package:

Install-Package MySql.Data

Register the Provider

Add the section to your app.config/web.config so that the .NET framework knows about the MySql.Data provider:

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

Make sure that the version specified matches the version you have installed or you will get an exception thrown by the .NET runtime at startup when it tries to resolve the assembly.

Connection String

Add a named connection string for your Database specifying the provider name.

<connectionStrings>
    <add name="MySQLTest"
         connectionString="Server=localhost;Database=database;Uid=User;Pwd=Pswd;"
         providerName="MySql.Data.MySqlClient" />
</connectionStrings>

Configuring the Connection

Create a session factory using the ForMySqlConnection extension method supplying the name of the connection string you added to the app.config/web.config.

using MicroLite;
using MicroLite.Configuration;

static void Main(string[] args)
{
    var sessionFactory = Configure
        .Fluently()
        .ForMySqlConnection("MySQLTest")
        .CreateSessionFactory();

    ...
}

Supported Identifier Strategies

MicroLite supports the following Identifier Strategies for MySQL:

  • Assigned
  • DbGenerated (using identity columns)

See the Getting Started guide for further details on using MicroLite.