Dapper Multi-Mapping

时间:2017-04-26 21:05:44

标签: c# dapper

在Dapper文档中,我可以清楚地看到半简单对象或简单对象的多映射。例如:

public class Post
{
     public int Id { get; set; }
     public string Name { get; set; }
     public User Owner { get; set; }
}

public class User
{
     public int Id { get; set; }
     public string Name { get; set; }
}

IDbConnection.QueryAsync<Post, User, Post>(query, (post, user) => { post.Owner = user; return post; }, new { Id = id }, null, true, "id", CommandType.Text);

问题是,如何让Dapper处理具有多个内部对象的更复杂的对象。例如:

public class PlotStemDomain
{
     public int Id { get; set; }
     ...
     public PlotDomain Plot { get; set; }
     public SpeciesDomain species { get; set; }
}

public class PlotDomain
{
     public int Id { get; set; }
     ...
}

public class SpeciesDomain
{
     public int Id { get; set; }
     ...
}

当它似乎只处理一个Func<Post, User, Post>时。

1 个答案:

答案 0 :(得分:1)

开箱即用Dapper的Multi-Mapping支持大约7个要映射的对象。另一种方法是使用QueryMultiple。请参阅此thread以获取示例,它非常相似...