dapper -multi-mapping:flat sql返回嵌套对象

Arn*_*j65 20 c# dapper

我有一个包含地址对象的公司.SQL返回是平的,我想让Query <>加载所有对象.

cnn.Query<Company,Mailing,Physical,Company>("Sproc", 
                    (org,mail,phy) =>
                    {
                        org.Mailing = mail;
                        org.Physical = phy;
                        return org;
                    },
                    new { ListOfPartyId = stringList }, null, true, commandTimeout: null,
                    commandType: CommandType.StoredProcedure, splitOn: "MailingId,PhyscialId").ToList();
Run Code Online (Sandbox Code Playgroud)

我不确定我是否也让SplitOn正确.我收到的消息是:

使用多映射API时,如果您具有Id参数名称以外的键,请确保设置splitOn参数:splitOn

建议会很棒.

Test.cs中的示例不是代码要求查询的参数.这些需要更新

Sam*_*ron 15

对我来说这很完美......也许是一个错字?

我看到PhyscialId哪个肯定看起来像一个.

class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Mailing Mailing { get; set; }
    public Physical Physical { get; set; }
}

class Mailing
{
    public int MailingId { get; set; }
    public string Name { get; set; }
}

class Physical
{
    public int PhysicalId { get; set; }
    public string Name { get; set; }
}

public void TestSOQuestion()
{
    string sql = @"select 1 as Id, 'hi' as Name, 1 as MailingId, 
       'bob' as Name, 2 as PhysicalId, 'bill' as Name";
    var item = connection.Query<Company, Mailing, Physical, Company>(sql,
                (org, mail, phy) =>
                {
                    org.Mailing = mail;
                    org.Physical = phy;
                    return org;
                },
                    splitOn: "MailingId,PhysicalId").First();


    item.Mailing.Name.IsEqualTo("bob");
    item.Physical.Name.IsEqualTo("bill");

}
Run Code Online (Sandbox Code Playgroud)