Dapper.Contrib and MiniProfiler (for MySql) integration issues

684 Views Asked by At

I'm trying to use MiniProfiler.Integrations.MySql along with Dapper.Contrib extensions to profile the sql queries sent to MySql server. I'm using my own ConnectionFactory:

public IDbConnection GetConnection()
{
    var connection = (DbConnection) new MySqlConnection(_connectionString);
    return new ProfiledDbConnection(connection, CustomDbProfiler.Current);
}

Dapper.Contrib allows inserting new records as simple as

public async Task AddAsync(TEntity sample)
{
    using (var connection = _connectionFactory.GetConnection())
    {
        await connection.InsertAsync(sample);
    }
}

But ProfiledDbConnection is interpreted as an SQLConnection, producing SQLServer syntax which is incompatible with MySQL:

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[Id], [CreatedAt], [AndSoOn]' at line 1

Looking for your advice on how to solve the issue and get MiniProfiler working.

I'm using (all from Nuget):

Dapper: 1.50.5
Dapper.Contrib: 1.50.5
MiniProfiler: 3.2.0
MiniProfiler.Integrations.MySql: 1.0.1

2

There are 2 best solutions below

0
On

Looks like I've found a workaround for the Insert() and InsertAsync() methods, they accept ISqlAdapter as an optional parameter which seems to fix the issue (but still I can't use this approach for Update()/UpdateAsync()). This is due to the fact that when you want to use MiniProfiler with MySQL and Dapper.Contrib you need to wrap the MySqlConnection which leads to Dapper.Contrib using the default (wrong) ISqlAdapter.

3
On

As a workaround, in the current version you can override the type-based name resolution in Dapper.Contrib like this:

SqlMapperExtensions.GetDatabaseType = conn => "MySqlConnection";

This will override the default connection.GetType()-based name behavior. Still, that's not awesome, and I'll take a look if we can improve this in the next Dapper release.