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

时间:2011-08-16 12:52:30

标签: 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();

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

  

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

建议会很棒。

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

1 个答案:

答案 0 :(得分: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");

}