c# - 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();
解决方案
我使用了 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;
}
}
推荐阅读
- vba - 尝试使用 excel VBA 访问网站上的数据,我需要能够单击按钮
- python-2.7 - Paramiko 在遇到缺少输出时挂起
- sql - 当到期日总是在每个月的最后一天时,查找不同的 ID
- c# - 如何在服务器端防止href属性的xss?
- android - Android 上的 Tensorflow 推理,找不到“迭代器”操作。没有注册 OpKernel 来支持 Op
- html - 在jsx onclick按钮中使用href
- php - PHP OCI BIND-IN & BIND-OUT 问题
- python - scapy 信标包的信号强度,负整数到二进制补码十六进制
- c++ - 对象赋值 - 无效使用成员函数错误
- javascript - HTML事件同时弹出,怎么选一个