dapper -multi-mapping:平面sql返回嵌套对象 [英] dapper -multi-mapping: flat sql return to nested objects

查看:98
本文介绍了dapper -multi-mapping:平面sql返回嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一家包含地址对象的公司。 SQL返回值是平坦的,我正试图让Query<>加载所有对象。

I have a company that contains an address object. The SQL return is flat, and I'm tring to get Query<> to load all the objects.

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是否正确。我收到消息:

I'm not sure if i have the SplitOn correct either. I'm getting the message:


使用多重映射API时,如果
有键,请确保设置splitOn参数参数名称之外的其他参数:splitOn

When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn

建议最好。

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

The examples in the Test.cs are not what the code asks for as parameters for the queries. These need to be updated

推荐答案

对我来说这很完美……也许是错字?

for me this works perfect ... perhaps a typo?

我看到 PhysicalId 肯定看起来像一个。

I see PhyscialId which definitely looks like one.

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");

}

这篇关于dapper -multi-mapping:平面sql返回嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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