简体   繁体   中英

Mapping with Dapper cascading objects

I'm refactoring an old query made with EF that's taking so much time. I was wondering with Dapper if I can automatically map such objects

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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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