2

我正在尝试使用 MiniProfiler.Integrations.MySql 以及 Dapper.Contrib 扩展来分析发送到 MySql 服务器的 sql 查询。我正在使用我自己的 ConnectionFactory:

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

Dapper.Contrib 允许插入新记录,就像

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

ProfiledDbConnection被解释为SQLConnection,产生与 MySQL 不兼容的 SQLServer 语法:

 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

寻求有关如何解决问题并使 MiniProfiler 正常工作的建议。

我正在使用(全部来自 Nuget):

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

4

2 回答 2

2

看起来我已经找到了Insert()and方法的解决InsertAsync()方法,它们接受 ISqlAdapter 作为可选参数,这似乎可以解决问题(但我仍然不能将这种方法用于Update()/ UpdateAsync())。这是因为当你想使用MiniProfilerwithMySQL并且Dapper.Contrib你需要包装MySqlConnection导致Dapper.Contrib使用 default (wrong)的事实ISqlAdapter

于 2018-05-29T10:28:37.393 回答
2

作为一种解决方法,在当前版本中,您可以像这样覆盖 Dapper.Contrib 中基于类型的名称解析:

SqlMapperExtensions.GetDatabaseType = conn => "MySqlConnection";

这将覆盖默认connection.GetType()的基于名称的行为。尽管如此,这并不令人敬畏,我会看看我们是否可以在下一个 Dapper 版本中改进它。

于 2018-06-12T00:40:00.403 回答