Multi Mapping in Dapper. Receiving the error in SpiltOn

dapper multimap

Question

*Can You explain the Split on function in the multimap *

I am Trying to get the data from the Database using Dapper ORM. I have received the following error System.ArgumentException : When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn

 public abstract class Domain
    {
       public Guid Id { get; set; }
    }


public abstract class ItemBase : Domain
  {
     private IList<Image> images = new List<Image>();
     public Guid? ParentId { get; set; }
     public string Name { get; set; }
     public IList<Image> Images { get { return images; } }
  }

 public class Meal : ItemBase
   {

   }

public class Item : ItemBase
  {
     private IList<Meal> meals = new List<Meal>();
     public IList<Meal> Meals { get { return meals; } };
  }


public class Image : Domain
  {
      public byte Img { get; set; }
       public string Description { get; set; }
   }



 public  class MealImageLink : Domain
 {
     public Guid ItemId { get; set; }
     public Guid ImageId { get; set; }
 }

/* search function to take dat from the table */

private List<Meal> SearchMeals(Guid id)
 {
   var query = @"SELECT meal.[Name],meal.[Description],meal.

   [Price],mealImage.[Image] as Img    
                                  FROM      [MealItems] as meal  
                                  LEFT JOIN   [MealImageLink] mealImageLink 

  on meal.Id= mealImageLink.MealItemId
                                  LEFT JOIN   [Images] mealImage on  

   mealImageLink.ImageId=mealImage.Id
                                  WHERE  meal.[ParentId]=@Id";

List<Meal> meals = ( _connection.Query<Meal, MealImageLink, Image, Meal>
                                       (query, (meal, mealLink, mealImage) =>
                                       {
                                           meal.Images.Add(mealImage);
                                           return meal;
                                       }, new { @Id = id })).ToList();
return meals;
}

Expert Answer

The multi-map feature is really more intended for scenarios like:

select foo.Id, foo.whatever, ...,
       bar.Id, bar.something, ...,
       blap.Id, blap.yada, ...
from foo ...
inner join bar ...
left outer join blap ...

or the lazier but not uncommon:

select foo.*, bar.*, blap.*
from ...
inner join bar ...
left outer join blap ...

But in both of these cases, there is a clear and obvious way to split the horizontal range into partitions; basically, whenever you see a column called Id, it is the next block. The name Id is configurable for convenience, and can be a delimited list of columns for scenarios where each table has a different primary key name (so User might have UserId, etc).

Your scenario seems quite different to this. It looks like you're currently only selecting 4 columns with no particular way of splitting them apart. I would suggest that in this case, it is easier to populate your model via a different API - in particular, the dynamic API:

var meals = new List<Meal>();
foreach(var row in _connection.Query(sql, new { @Id = id }))
{
    string name = row.Name, description = row.Description;
    decimal price = row.Price;
    // etc

    Meal meal = // TODO: build a new Meal object from those pieces
    meals.Add(meal);
}

The dynamic API is accessed simply by not specifying any <...>. With that done, columns are accessed by name, with their types implied by what they are being assigned to - hence things like:

decimal price = row.Price;

Note: if you want to consume the row data "inline", then just cast as soon as possible, i.e.

// bad: forces everything to use dynamic for too long
new Meal(row.Name, row.Description, row.Price);

// good: types are nailed down immediately
new Meal((string)row.Name, (string)row.Description, (decimal)row.Price);

Does that help?

Tl;dr: I just don't think multi-mapping is relevant to your query.


Edit: here's my best guess at what you intend to do - it simply isn't a good fit for multi-map:

var meals = new List<Meal>();
foreach (var row in _connection.Query(query, new { @Id = id })) {
    meals.Add(new Meal {
        Name = (string)row.Name,
        Images = {
            new Image {
                Description = (string)row.Description,
                Img = (byte)row.Img
            }
        }
    });
}
return meals;


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why