Dapper查询给了我错误的设置

时间:2015-07-10 12:36:45

标签: c# asp.net-mvc dapper micro-orm

我的dapper查询类似这样的

const string sql = "SELECT d.*,p.*" +
   " FROM Duvar d INNER JOIN Post p ON p.DuvarId=d.Id " +
   "WHERE d.UserId=@UserId  ORDER BY p.PostDate";
_connection.Open();
var duvarm = await  _connection.QueryAsync<Duvar,Post,Duvar>(sql, (d, p) =>
{
    if (d.Post == null)
        d.Post = new List<Post>();
    d.Post.Add(p);
    return d;

}, new { UserId = userId });
return duvarm.FirstOrDefault();

我的Duvar模型就像这样

public class Duvar
{
    public int Id { get; set; }
    public int UserId { get; set; }

    public List<Post> Post { get; set; }
}

我的帖子模型是

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime PostDate { get; set; }

    public List<Comment> Comment { get; set; }

    public int PostYapanUserId { get; set; }
    public int DuvarId { get; set; }
    public int Rate { get; set; }
    public int UpVote { get; set; }
    public int DownVote { get; set; }
}

当我尝试运行此代码时,我在这里获得结果(return duvarm.FirstOrDefault();)2 Duvar模型和每个Duvar模型内部填充Post模型。但这不是我所感谢的。我期待的是一个Duvar这个Duvar模型中的模型和polulated post模型。 有人这样想:new Duvar { new Post{....}, new Post{...} } 但我得到的是:new Duvar{new Post{...}},new Duvar {new Post{ ...}}

0 个答案:

没有答案