Dapper多重映射问题 [英] Dapper Multi-mapping Issue

查看:325
本文介绍了Dapper多重映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码块,请继续执行在使用多重映射API时,如果您具有ID以外的键,请确保设置splitOn参数错误:

Keep running into "When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id" error for the below code-block:

var accounts = DbConnection.Query<Account, Branch, Application, Account>(
            "select Accounts.*, SplitAccount = '', Branches.*, SplitBranch = '', Applications.*" +
            " from Accounts" +
            "    join Branches" +
            "       on Accounts.BranchId = Branches.BranchId" +
            "    join Applications" +
            "       on Accounts.ApplicationId = Applications.ApplicationId" +
            " where Accounts.AccountId <> 0",
            (account, branch, application) =>
                {
                    account.Branch = branch;
                    account.Application = application;
                    return account;
                }, splitOn : "SplitAccount, SplitBranch"
            ).AsQueryable();

我正在使用SplitAccount和SplitBranch进行splitOn作为解决方法。

I'm using SplitAccount and SplitBranch for splitOn as a workaround.

我错过了什么吗?

谢谢

编辑:

我已经清理了一下测试,下面是一个简单的类和一个新查询:

I have cleaned up my test a little, below is a light version of classes and a new query:

public class AccountLight
{
    public int AccountId { get; set; }
    public string AccountNumber { get; set; }
    public BranchLight Branch { get; set; }
    public ApplicationLight Application { get; set; }
}

public class BranchLight
{
    public int BranchId { get; set; }
    public string BranchNumber { get; set; }
}

public class ApplicationLight
{
    public int ApplicationId { get; set; }
    public string ApplicationCode { get; set; }
}

var accounts2 = DbConnection.Query<AccountLight, BranchLight, ApplicationLight, AccountLight>(
    "select Accounts.AccountId, Accounts.AccountNumber," +
    "       Branches.BranchId, Branches.BranchNumber," +
    "       Applications.ApplicationId, Applications.ApplicationCode" +
    " from Accounts" +
    "    inner join Branches" +
    "       on Accounts.BranchId = Branches.BranchId" +
    "    inner join Applications" +
    "       on Accounts.ApplicationId = Applications.ApplicationId" +
    " where Accounts.AccountId <> 0",
    (account, brach, application) =>
    {
        account.Branch = brach;
        account.Application = application;
        return account;
    }, 
    commandType: CommandType.Text,
    splitOn: "AccountId, BranchId"
    ).AsQueryable();


推荐答案

在调试了Dapper的源代码几个小时之后,我终于发现了问题,这很有趣。

After few hours of debugging Dapper's source code, I finally found the issue and it is quite interesting one.

提供多个splitOn字段时,Dapper会基于逗号进行拆分,例如var splits = splitOn.Split(’,’)。ToArray()。然后,它遍历所有记录集字段,并根据上述数组将其拆分为对象;

When multiple splitOn fields are supplied, Dapper does a split based on comma, e.g. var splits = splitOn.Split(',').ToArray(). Then it loops through all record-set fields and split’s them up into objects based on the above array; pretty strait forward.

现在有趣的部分:当我提供了splitOn字段时,在逗号后有一个额外的空格,例如原因是 AccountId,BranchId,这是很小的空间。在Split()之后,BranchId字段包含多余的空间,无法与记录集中的任何字段匹配。

Now the fun part: When I supplied my splitOn fields, I had an extra SPACE after the comma, e.g. "AccountId, BranchId" and that little space was the cause. After Split(), BranchId field contained an extra space and failed to match with ANY fields in the record-set.

有两种解决方法:


  1. 逗号后不要使用多余的空格;我个人上瘾了
    ; SQL的一个旧习惯。

  2. 修改Dapper的GenerateDeserializers
    方法并更改:var currentSplit = splits [splitIndex]更改为var
    currentSplit = splits [splitIndex] .Trim()或类似的方法;这就是我对本地副本所做的。

这是代码快照:

    private static Func<IDataReader, object>[] GenerateDeserializers(Type[] types, string splitOn, IDataReader reader)
    {
        int current = 0;
        var splits = splitOn.Split(',').ToArray();
        var splitIndex = 0;

        Func<Type, int> nextSplit = type =>
        {
            var currentSplit = splits[splitIndex].Trim();
            if (splits.Length > splitIndex + 1)
            {
                splitIndex++;
            }

更新:

以上修复程序已合并: https://github.com/SamSaffron/dapper- dot-net / commit / 399db17e5aa6f1eefaf8fdccff827020be8e6cbb

The above fix got merged: https://github.com/SamSaffron/dapper-dot-net/commit/399db17e5aa6f1eefaf8fdccff827020be8e6cbb

这篇关于Dapper多重映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆