【问题标题】:dapper -multi-mapping: flat sql return to nested objectsdapper -multi-mapping: flat sql 返回嵌套对象
【发布时间】:2012-05-22 18:20:40
【问题描述】:

我有一个包含地址对象的公司。 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 参数,如果 您有除 Id 以外的键参数名称:splitOn

建议会很棒。

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

【问题讨论】:

  • 你能发布 sproc 调用返回的结果集列吗?您需要确保 SplitOn 中的列存在于结果集中
  • MailingId 返回。 proc 的所有返回值都是对象的属性/字段。
  • dapper 中多重映射的最佳答案。 stackoverflow.com/questions/7472088/…

标签: c# dapper


【解决方案1】:

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

我看到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");

}

【讨论】:

  • 我误解了 SplitOn 正在拆分由这些值设置的返回值。它现在正在工作。但是,我无法使用上面的确切语法。我需要提供 CommandType、缓冲区标志等。
  • 我想知道如果数据库查询具有左外连接并且物理信息可能不存在/为空,它会起作用。这还能用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多