小巧玲珑的多重映射。在SpiltOn中接收错误

时间:2016-02-25 07:28:12

标签: dapper multimap

*您能解释多图*中的拆分功能

我正在尝试使用Dapper ORM从数据库中获取数据。我收到以下错误 System.ArgumentException:使用多映射API时,如果您具有Id以外的密钥,请确保设置splitOn参数 参数名称: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; }
 }

/ *搜索函数从表* /

中获取数据
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;
}

1 个答案:

答案 0 :(得分:1)

多地图功能更适用于以下场景:

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

或更懒惰,但并非罕见:

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

但是在这两种情况下,有一种明确而明显的方法可以将水平范围分割成分区;基本上,每当你看到一个名为Id的列时,它就是下一个块。名称Id可配置为方便起见,并且可以是列的分隔列表,其中每个表具有不同的主键名称(因此User可能具有UserId等)。< / p>

您的方案似乎与此截然不同。看起来您目前只选择4列而没有特别的方法将它们分开。我建议在这种情况下,通过不同的API更容易填充模型 - 特别是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);
}

只需不指定任何dynamic即可访问<...> API。完成后,将按名称访问列,其类型隐含,它们被分配给 - 因此类似于:

decimal price = row.Price;

注意:如果您想使用row数据&#34;内联&#34;,那么只需尽快 ,即

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

这有帮助吗?

Tl;博士:我不认为多映射与您的查询相关。

编辑:这是我对你打算做什么的最好猜测 - 它根本不适合多地图:

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;