SilentRage47
SilentRage47

Reputation: 952

Dapper MultiMapping not working

I'm trying to create a List of object Work using Dapper to do the mapping.

This is the code:

public class Work
{
  public int id { get; set; }
  public int id_section { get; set; }
  public decimal price { get; set; }
  public Model id_model { get; set; }
  public Type id_type { get; set; }
}

class Model
{
    public int id_model { get; set; }
    public string Name { get; set; }
}

class Type
{
    public int id_type { get; set; }
    public string Name { get; set; }
}

public List<Work> GetListOfWork(int idList)
{
using (DatabaseConnection db = new DatabaseConnection()) //return a connection to MySQL
{
     var par = new {Id =  idList};
     const string query = "SELECT id,id_section,price,id_model,id_type FROM table WHERE id_section = @Id";
     return db.con.Query<Work, Model, Type, Work>(query,
          (w, m, t) =>
          {
                w.id_model = m;
                w.id_type = t;
                return w;
          }, par, splitOn: "id_model,id_type").ToList();
}
}

It doesn't give me any error but id_model and id_type in my the returned List are always empty (The object are created but all the fields are empty or null), other fields are mapped correctly.

Any clue ?

Upvotes: 0

Views: 787

Answers (1)

Steve
Steve

Reputation: 216243

You need to add yourself the joins in the query string Probably it is something like this

 var par = new {Id =  idList};
 const string query = @"SELECT w.id,w.id_section,w.price,
                        m.id_model, m.Name, t.id_type, t.Name
                        FROM work w INNER JOIN model m on w.id_model = m.id_model
                                    INNER JOIN type t on w.id_type = t.id_type
                        WHERE w.id_section = @Id";
 return db.con.Query<Work, Model, Type, Work>(query,
      (w, m, t) =>
      {
            w.id_model = m;
            w.id_type = t;
            return w;
      }, par, splitOn: "id_model,id_type").ToList();

Upvotes: 1

Related Questions