ここでDapperの新機能!マルチマッピングに問題があります。これは私のクエリです:
var sql = @"select distinct a.*,
c.Id as 'GenreId', c.Active as 'GenreActive', c.Link as 'GenreLink', c.Name as 'GenreName', c.DateCreated as 'GenreDateCreated', c.DateEdited as 'GenreDateEdited',
d.Id as 'CommentId', d.ReviewId as 'CommentReviewId', d.Name as 'CommentName', d.Email as 'Comment.Email', d.Content as 'CommentContent', d.Active as 'CommentActive', d.DateCreated as 'CommentDateCreated', d.DateEdited as 'CommentDateEdited', d.CommentId as 'ReplyCommentId'
from Review a " +
"left join ReviewGenre b on a.Id = b.ReviewId " +
"left join Genre c on c.Id = b.ReviewId " +
"left join Comment d on a.Id = d.ReviewId " +
"where a.Active = 1 " +
"order by a.DatePublished desc;"
;
そして、私のエンティティは(簡潔にするために短縮されています):
public class Review
{
public int Id {get;set;}
public IEnumerable<Genre> Genres { get; set; }
public IEnumerable<Comment> Comments { get; set; }
}
public class Genre
{
public int Id {get;set;}
public string Name {get;set;}
}
public class Comment
{
public int Id {get;set;}
public int Content {get;set;
}
Dapper を使用した私のクエリは、Genre.Id と Comment.Id の名前が変更された列で分割しようとします。ジャンルとコメントのいずれも Review クラスにマッピングされていないように見えることを除いて、クエリは正常に機能しているように見えます。これは私がクエリを実行しようとしている方法です:
using (var connection = new SqlConnection(_ConnectionString))
{
var reviews = await connection.QueryAsync<Review, Genre, Comment, Review>(
sql,
(review, genre, comment) =>
{
review.Genres = new List<Genre>();
review.Comments = new List<Comment>();
if (genre != null)
{
review.Genres.ToList().Add(genre);
}
if (comment != null)
{
review.Comments.ToList().Add(comment);
}
return review;
},
commandType: CommandType.Text,
splitOn: "GenreId,CommentId");
return reviews;
}
私はこの件に関するチュートリアルとSO全体を調査しましたが、マッピングが発生しない原因を見つけられませんでした。
提案をいただければ幸いです(Dapperの初心者)。ありがとう!