Skip to content

Three level mapping issue #71

@JemCasey

Description

@JemCasey

Hello,

I'm having an issue where the third level of a mapping isn't working properly, with the collection on the second level always coming back as null. My classes are as follows:

    public class AlbumList
    {
        public int AlbumListId { get; set; }
        public string Name { get; set; }
        public string SourceUrl { get; set; }

        public IList<Album> Albums { get; set; }
    }

    public class Album
    {
        public int AlbumId { get; set; }
        public int ArtistId { get; set; }
        public string Name { get; set; }
        public string Artist { get; set; }
        public int Rank { get; set; }

        IList<Genre> Genres { get; set; }
    }

    public class Genre 
    {
        public int GenreId { get; set; }
        public int Name { get; set; }
    }

And my methods are:

       public AlbumList Get(int id)
        {
            var sql = @"SELECT TOP 1 AlbumList.AlbumListId
	                    , AlbumList.Name
	                    , AlbumList.SourceUrl
	                    , Album.AlbumId AS Albums_AlbumId
	                    , Album.Name AS Albums_Name
	                    , AlbumListEntry.Rank AS Albums_Rank
	                    , Artist.Name AS Albums_Artist
                        , Genre.GenreId AS Albums_Genres_GenreId
                        , Genre.Name AS Albums_Genres_Name
	                    FROM AlbumList
		                    JOIN AlbumListEntry ON AlbumList.AlbumListId = AlbumListEntry.AlbumListId
		                    JOIN Album ON AlbumListEntry.AlbumId = Album.AlbumId
		                    JOIN Artist ON Album.ArtistId = Artist.ArtistId
                            LEFT JOIN AlbumGenre ON Album.AlbumId = AlbumGenre.AlbumId
                            LEFT JOIN Genre ON AlbumGenre.GenreId = Genre.GenreId
                        WHERE AlbumList.AlbumListId = @id
	                    ORDER BY AlbumListEntry.Rank";
           
            return albumListRepository.Query<Album, Genre>(sql, "AlbumListId,AlbumId,GenreId", new { id }).FirstOrDefault();
        }

And, in my repository class:

public List<T> Query<T2, T3>(string sql, string splitOn, object param = null)
        {
            using (var conn = GetOpenConnection())
            {
                var results = conn.Query<dynamic>(sql, param);

                Slapper.AutoMapper.Cache.ClearInstanceCache();

                var ids = splitOn.Split(',');

                Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(T), new List<string> { ids[0] });
                Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(T2), new List<string> { ids[1] });
                Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(T3), new List<string> { ids[2] });

                var slapperResults = (Slapper.AutoMapper.MapDynamic<T>(results) as IEnumerable<T>).ToList();

                return slapperResults.ToList();
            }
        }

The first level of mapping works perfectly; I get an album list with a full list of albums, with all the expected properties filled properly. However, no matter what adjustments I make I can't get the album's IList of genres to be anything but null, despite the fact that the genre data is getting returned from the query properly and appears in the dynamic results. Any suggestions about what I might be doing wrong would be greatly appreciated; thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions