【问题标题】:How to select one to many relationship & one to one together using dapper如何使用 dapper 选择一对多关系和一对一关系
【发布时间】: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


【解决方案1】:

所以我最终做了如下的事情。

public static IEnumerable<Post> Map(this SqlMapper.GridReader reader, Func<Post, int> postKey, Func<Comment, int> commentKey, Func<Tag, int> TagKey, Action<Post, IEnumerable<Comment>> addPostComment, Action<Post, IEnumerable<Tag>> addPostTag)
{
    var posts = reader.Read<Post>().ToList();
    var comments = reader.Read<Comment>().GroupBy(t => commentKey(t)).ToDictionary(g => g.Key, g => g.AsEnumerable());
    var tags = reader.Read<Tag>().GroupBy(t => TagKey(t)).ToDictionary(g => g.Key, g => g.AsEnumerable());
    var authors = reader.Read<Author>().ToList();

    foreach (var post in posts)
    {
        IEnumerable<Tag> posttags;
        if (tags.TryGetValue(postKey(post), out posttags))
        {
            addPostTag(post, posttags);
        }

        IEnumerable<Comment> postcomments;
        if (comments.TryGetValue(postKey(post), out postcomments))
        {
            addPostComment(post, postcomments);
        }

        post.Author = authors.Where(a => a.AuthorId == post.AuthorId).SingleOrDefault();
    }
    return posts;
}

使用上面的扩展如下。

        var query = "SELECT p.* FROM Post p " +
                    "SELECT c.* FROM Comment c WHERE c.PostId in ( SELECT PostId FROM Post) " +
                    "SELECT t.* FROM Tag t WHERE t.PostId in ( SELECT PostId FROM Post) " +
                    "SELECT a.* FROM Author a WHERE a.AuthorId in ( SELECT AuthorId FROM Post) ";
        List<Post> result = new List<Post>();
        using (var conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
        {
            conn.Open();
            try
            {
                var posts = conn.QueryMultiple(query)
                            .Map
                            (
                                p => p.PostId,
                                c => c.PostId,
                                t => t.PostId,
                                (p, c) => p.Comments.AddRange(c),
                                (p, t) => p.Tags.AddRange(t)
                            );
                if (posts != null && posts.Any())
                {
                    result.AddRange(posts);
                }
            }
            catch (Exception)
            {
                //TODO: Log Exception
                throw;
            }
            conn.Close();
        }

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2020-12-15
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多