Cod*_*ure 5 c# sql-server dapper
我正在尝试将对象从 sql 映射到 C# 中的多个对象。在具有 QueryFirstAsync 和 QueryFirstOrDefaultAsync 的情况下如何进行多重映射?
我尝试过类似于 QueryAsync 或 Query 中的过程。
ClassA record =
await this.dbConnection.QueryFirstAsync<ClassA, ClassB, ClassA>(
sql,
(a, b) =>
{
a.Id = b.Id;
return a;
},
splitOn: "Id",
param: new
{
memberId
});
Run Code Online (Sandbox Code Playgroud)
我期望构建成功,但它不适用于 QueryFirstAsync 或 QueryFirstOrDefaultAsync。
当前的问题:
您正在尝试将多重映射与 dapper 函数QueryFirstAsync和 一起使用QueryFirstOrDefaultAsync,但它们的重载都不支持 Mutli-Mapping,如 所做的那样QueryAsync,粘贴来自 Dapper 代码的定义:
public static Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TReturn>(this IDbConnection cnn,
CommandDefinition command, Func<TFirst, TSecond, TReturn> map, string splitOn = "Id") =>
MultiMapAsync<TFirst, TSecond, DontMap, DontMap, DontMap, DontMap, DontMap, TReturn>(cnn,
command, map, splitOn);
Run Code Online (Sandbox Code Playgroud)
它调用MultiMapAsync,它利用该SplitOn值创建多地图默认值是Id
private static async Task<IEnumerable<TReturn>> MultiMapAsync<TReturn>(this IDbConnection cnn, CommandDefinition command, Type[] types, Func<object[], TReturn> map, string splitOn)
{
if (types.Length < 1)
{
throw new ArgumentException("you must provide at least one type to deserialize");
}
object param = command.Parameters;
var identity = new Identity(command.CommandText, command.CommandType, cnn, types[0], param?.GetType(), types);
var info = GetCacheInfo(identity, param, command.AddToCache);
bool wasClosed = cnn.State == ConnectionState.Closed;
try
{
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
using (var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader))
using (var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, command.CancellationToken).ConfigureAwait(false))
{
var results = MultiMapImpl(null, default(CommandDefinition), types, map, splitOn, reader, identity, true);
return command.Buffered ? results.ToList() : results;
}
}
finally
{
if (wasClosed) cnn.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案:
使用 standard QueryAsync,它有一个重载,SplitOn在最终结果调用上采用 和FirstorDefault,因为 Dapper 查询的结果是IEnumerable<T>,因此可以调用任何标准 Linq 扩展方法
| 归档时间: |
|
| 查看次数: |
3707 次 |
| 最近记录: |