首页 > 解决方案 > C# Dapper 查询中的多个实体:参数过多

问题描述

我使用 dapper 通过存储过程查询我的数据库。真正的问题是我的 sp 返回 5 个实体,所以当我想要 map 时Dapper返回一个错误:

委托 Func<..> 不接受 5 个参数

我想使用一个存储过程,因为它很复杂,有很多连接。

var queryParameters = new DynamicParameters();
queryParameters.Add("@no_dossier", numero_dossier);
queryParameters.Add("@utilisateur_magasin_id", utilisateur_id);
var result =
    db.Query<SocieteContactDTO, AppareilClientDTO, AppareilDTO, MarqueDTO, AppareilEnseigneDTO, DossierFrontDTO, SocieteContactDTO>("ps_appareil_client_by_client_id_pour_recherche_front",
        (societecontact, appclient, app, marque, dossierfront) =>
        {                   
            return societecontact;
        }, queryParameters, splitOn: "appareil_id,appareil_client_id, marques_id, code_produit", commandType: CommandType.StoredProcedure)
      .ToList();

标签: c#mappingmultiple-columnsdapper

解决方案


我使用了 dapper,它在 fun<> 委托中最多支持七个实体。您能否更新 dapper 版本我不确定您使用的是哪个版本,但使用的是 v 1.60.0,它对我来说很好用。

 public async Task<IEnumerable<TResult>> ExecuteReaderAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TResult>(
        TDbConnection connection,
        CommandType cmdType,
        string cmdText,
        TDbParameter commandParameters,
        Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh,TResult> map,
        string splitOn = "Id")
    {

        try
        {
            using (var _connection = connection)
            {
                _connection.Open();

                return await connection.QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TResult>(cmdText, map, commandParameters, splitOn: splitOn, commandType: cmdType);
            }
        }

        catch (Exception ex)
        {
            throw;
        }
    }

推荐阅读