Dapper Multi映射不正确

时间:2019-04-20 19:29:46

标签: c# dapper

我正在尝试使用Dapper映射两个实体。就像这样:

您有台电影: MovieID, 运行, 描述, 标题, 导向器, 风格

和表投影: 投影ID, 时间, 价钱, 大厅, MovieID

电影具有投影列表(在每部电影之前有一个或多个投影)

连接后,具有投影效果的电影的结果列表只是第一部电影和来自投影的所有记录。

基本上,Movie1具有两个投影Movie2具有三个投影,它返回五个电影的列表,并且所有这些都是第一部电影,并且它们都具有五个投影。

有人可以帮助我在哪里出错?

public List<Movie> ReturnMovieProjections()
{
    var list = new List<Movie>();
    var sql = "SELECT " +
                        "m.MovieID, " +
                        "m.Runtime, " +
                        "m.Description, " +
                        "m.Title, " +
                        "m.Director, " +
                        "m.Genres, " +
                        "p.MovieID, " +
                        "p.ProjectionID, " +
                        "p.Time, " +
                        "p.Price, " +
                        "p.Hall " +
                  "FROM Movie AS m INNER JOIN Projection AS p " +
                    "ON m.MovieID = p.MovieID " +
                  "WHERE p.MovieID = m.MovieID";

     using (var connection = new OleDbConnection(GetConnectionString("CinemaDB")))
     {
         var movieDictionary = new Dictionary<int, Movie>();
         list = connection.Query<Movie, Projection, Movie>(
         sql, (movie, projection) =>
         {
             Movie movieEntry;
             if (!movieDictionary.TryGetValue(movie.MovieID, out movieEntry))
             {
                 movieEntry = movie;
                 movieEntry.Projections = new List<Projection>();
                 movieDictionary.Add(movieEntry.MovieID, movieEntry);
             }
             movieEntry.Projections.Add(projection);
             return movieEntry;
         },
         splitOn: "p.MovieID").AsList();
        }
        return list;
    }

1 个答案:

答案 0 :(得分:0)

您应该返回字典值,而不是Dapper获得的IEnumerable。
该值将始终包含查询生成的所有5条记录

也无需添加等于JOIN语句的WHERE条件

var sql = "SELECT " +
                "m.MovieID, " +
                "m.Runtime, " +
                "m.Description, " +
                "m.Title, " +
                "m.Director, " +
                "m.Genres, " +
                "p.MovieID, " +
                "p.ProjectionID, " +
                "p.Time, " +
                "p.Price, " +
                "p.Hall " +
          "FROM Movie AS m INNER JOIN Projection AS p " +
            "ON m.MovieID = p.MovieID ";
 using (var connection = new OleDbConnection(GetConnectionString("CinemaDB")))
{
    var movieDictionary = new Dictionary<int, Movie>();
    list = connection.Query<Movie, Projection, Movie>(sql, (movie, projection) =>
    {
         Movie movieEntry = null;
         if (!movieDictionary.TryGetValue(movie.MovieID, out movieEntry))
         {
             movieEntry = movie;
             movieEntry.Projections = new List<Projection>();
             movieDictionary.Add(movieEntry.MovieID, movieEntry);
         }
         movieEntry.Projections.Add(projection);
         return movie; // return the same instance passed by Dapper.
    },splitOn: "p.MovieID");
}
return movieDictionary.Values.ToList();