简体   繁体   English

使用 Dapper 级联对象映射

[英]Mapping with Dapper cascading objects

I'm refactoring an old query made with EF that's taking so much time.我正在重构一个用 EF 制作的旧查询,它花费了很多时间。 I was wondering with Dapper if I can automatically map such objects我想知道 Dapper 是否可以自动映射这些对象

public class Chest
{
  public Item Item {get;set;}
}

public class Item
{
   public IList<Property> Properties {get;set;}
}

public class Property
{
    public int Id {get;set;}
    public string Description {get;set;}
}

Is there a way I can retrieve all those items as I would do with EF?有没有办法可以像使用 EF 一样检索所有这些项目?

I've seen the Query and so on but I don't understand if it meets the case我看过查询等等,但我不明白它是否符合这种情况

Your model is pretty straight forward, since there's only 1 collection - IList<Property> , let's assume your query is Select Id, Description from PropertyTable , then using Dapper, you can do the following:您的模型非常简单,因为只有 1 个集合 - IList<Property> ,假设您的查询是Select Id, Description from PropertyTable ,然后使用 Dapper,您可以执行以下操作:

IList<Property> PropertyList = conn.Query<Property>("Select Id, Description from PropertyTable").ToList();

After that its simple assignment:之后它的简单分配:

Chest chest = new Chest{Item = new Item{Properties = PropertyList}};

This still need extra assignment, since from Dapper you get IEnumerable<T> as result, there could be a Dapper Extension , which can directly fill the Chest object, if you provide explicit object mapping, though in my view its not required, since the solution is simple这仍然需要额外的分配,因为从 Dapper 你得到IEnumerable<T>结果,可能有一个Dapper Extension ,它可以直接填充Chest对象,如果你提供明确的对象映射,尽管在我看来它不是必需的,因为解决方案很简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM