【发布时间】:2011-07-27 03:31:00
【问题描述】:
我有以下类和数据库架构。我正在尝试使用 dapper 从数据库中查询这些数据,这将水合完整的对象图。我查看了各种 SO 问题和测试,但无法真正弄清楚如何做到这一点。
数据库架构
Author
-AuthorId
-Name
Post
-PostId
-Content
-AuthorId
Comment
-PostId
-CommentId
-Content
Tag
-PostId
-TagId
-Name
类
public class Author
{
public int AuthorId { get; set; }
public string Name { get; set; }
}
public class Tag
{
public int PostId { get; set; }
public int TagId { get; set; }
public string Name { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Content { get; set; }
public int AuthorId { get; set; }
public List<Tag> Tags { get; set; }
public List<Comment> Comments { get; set; }
public Author Author { get; set; }
public Post()
{
this.Comments = new List<Comment>();
this.Tags = new List<Tag>();
}
}
public class Comment
{
public int PostId { get; set; }
public int CommentId { get; set; }
public string Content { get; set; }
}
【问题讨论】:
-
我在这里介绍了一般模式:stackoverflow.com/questions/6146918/…
-
Sam,感谢您的参考,它确实有很大帮助。虽然我认为我对我的用例不是很清楚。我已经更新了解决方案以更详细地反映它。我实际上在图中既有一对一的关系,也有一对多的关系。发布->作者,发布->评论->标签。从您提供的解决方案 Entity -> Childrens 可以解决您将如何解决 Entity -> Child & Entity -> Children -> Children 用例。
-
现在我只是手动映射 int 而不是试图概括解决方案
-
你能发布一些关于你如何手动映射的代码吗?我正在解决类似的问题。
标签: select one-to-many one-to-one dapper