Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dapper -multi-mapping: flat sql return to nested objects

Tags:

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

like image 941
Omnia9 Avatar asked Aug 16 '11 12:08

Omnia9


People also ask

What does dapper execute return?

Dapper provides the Execute method (and its async equivalent) for commands that are not intended to return resultsets i.e. INSERT , UPDATE and DELETE commands. The Execute method returns an int , representing the number of rows affected by the successful completion of the command.

Why Dapper is micro ORM?

Dapper is an example of Micro ORM, in fact, it is called the King of Micro ORM because of its speed and ease of work. First, it creates an IDbConnection object and allows us to write queries to perform CRUD operations on the database.

Is Dapper A ORM?

Dapper is an object–relational mapping (ORM) product for the Microsoft . NET platform: it provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks.


1 Answers

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

}
like image 154
Sam Saffron Avatar answered Sep 22 '22 14:09

Sam Saffron