20

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

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

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

Suggestions would be great.

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

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
Omnia9
  • 1,563
  • 4
  • 14
  • 39
  • 1
    Can you post the result set columns that are returned by the sproc call? You need to make sure the columns in SplitOn exist in the result set – bdowden Aug 16 '11 at 15:22
  • 2
    MailingId is returned. All the return values of the proc are the properties/fields of the objects. – Omnia9 Aug 16 '11 at 15:41
  • 1
    Best answer for multimapping in dapper. http://stackoverflow.com/questions/7472088/correct-use-of-multimapping-in-dapper/7478958#7478958 – CPhelefu Dec 03 '15 at 17:18

1 Answers1

15

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

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

}
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • I misunderstood that the SplitOn is splitting the return set by those values. It is working now. However, I am not able to use the exact syntax as you have above. I need to give the CommandType, the buffer flag etc. – Omnia9 Aug 17 '11 at 11:41
  • 1
    I was wondering would it work if the database query had a left outer join and the physical info could be non-existant/null. Would this still work? – Marko Oct 05 '12 at 01:23