44
I'm trying to use the Multi-mapping feature of dapper to return a list of Album and associated Artist and Genre.
public class Artist
{
public virtual int ArtistId { get; set; }
public virtual string Name { get; set; }
}
public class Genre
{
public virtual int GenreId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
public class Album
{
public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
var query = @"SELECT AL.Title, AL.Price, AL.AlbumArtUrl, GE.Name, GE.[Description], AR.Name FROM Album AL INNER JOIN Genre GE ON AL.GenreId = GE.GenreId INNER JOIN Artist AR ON AL.ArtistId = AL.ArtistId";
var res = connection.Query<Album, Genre, Artist, Album>(query, (album, genre, artist) => { album.Genre = genre; album.Artist = artist; return album; }, commandType: CommandType.Text, splitOn: "ArtistId, GenreId");
I have checked for solution regarding this, non of it worked. Can anyone please let me know where I have gone wrong?
Thanks @Alex :) But I am still struck. This is what I have done:
CREATE TABLE Artist
(
ArtistId INT PRIMARY KEY IDENTITY(1,1)
,Name VARCHAR(50)
)
CREATE TABLE Genre
(
GenreId INT PRIMARY KEY IDENTITY(1,1)
,Name VARCHAR(20)
,[Description] VARCHAR(1000)
)
CREATE TABLE Album
(
AlbumId INT PRIMARY KEY IDENTITY(1,1)
,GenreId INT FOREIGN KEY REFERENCES Genre(GenreId)
,ArtistId INT FOREIGN KEY REFERENCES Artist(ArtistId)
,Title VARCHAR(100)
,Price FLOAT
,AlbumArtUrl VARCHAR(300)
)
INSERT INTO Artist(Name) VALUES ('Jayant')
INSERT INTO Genre(Name,[Description]) VALUES ('Rock','Originally created during sch