C# 使用多映射 API 时,如果您有 Id 以外的键,请确保设置 splitOn 参数", "splitOn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17168926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:45:08  来源:igfitidea点击:

When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id", "splitOn

c#dapper

提问by user2497013

I'm trying to use the Multi-mapping feature of dapper to return a list of Album and associated Artist and Genre.

我正在尝试使用 dapper 的多映射功能来返回专辑列表以及相关的艺术家和流派。

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:

谢谢@Alex :) 但我仍然很震惊。这就是我所做的:

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 school days. The year was.....I guess 1998')
DECLARE @gen_id INT
        ,@art_id INT
SET @gen_id = (SELECT MAX(GenreId) FROM Genre)
SET @art_id = (SELECT MAX(ArtistId) FROM Artist)
INSERT INTO Album(GenreId,ArtistId,Title,Price,AlbumArtUrl) VALUES (@gen_id,@art_id,'I go mad for you',200,'http://asha4u.com/IGoMad')

As suggested by you I changed the query to:

按照您的建议,我将查询更改为:

var query = @"SELECT AL.AlbumId, AL.Title, AL.Price, AL.AlbumArtUrl, GE.GenreId, GE.Name, GE.Description, AR.ArtistId, AR.Name FROM Album AL INNER JOIN Artist AR ON AR.ArtistId = AL.ArtistId INNER JOIN Genre GE ON GE.GenreId = AL.GenreId";

var res = connection.Query<Album, Genre, Artist, Album>(query, (album, genre, artist) => { album.Genre = genre; album.Artist = artist; return album; }, commandType: CommandType.Text, splitOn: "GenreId, ArtistId");

Now I am using splitOn for GenreId and ArtistId. I still get the same error. Please help.

现在我将 splitOn 用于 GenreId 和 ArtistId。我仍然遇到同样的错误。请帮忙。

回答by Alex

You need to include the column you'd want to split on in your select query. Yours just selects all the other properties - so Dapperdoesn't find a matching column to split the objects.

您需要在选择查询中包含要拆分的列。您的只是选择了所有其他属性 - 所以Dapper没有找到匹配的列来拆分对象。

Your query should probably be something like that:

您的查询可能应该是这样的:

var query = @"SELECT AlbumId, Title, Price, AlbumArtUrl, GenreId, Name, Description , ArtistId, Name ......" etc

Sam wrote an excellent answer for multi mappings and the splitOn option: https://stackoverflow.com/a/7478958/1028323

Sam 为多映射和 splitOn 选项写了一个很好的答案:https://stackoverflow.com/a/7478958/1028323

Edit: If your query is as mentioned above, you'll have to split on GenreId and ArtistId.

编辑:如果您的查询如上所述,则必须拆分 GenreId 和 ArtistId。

 AlbumId, Title, Price, AlbumArtUrl | GenreId, Name, Description | ArtistId, Name

The pipes are for the start of a new POCO you're trying to map. So the SplitOnparameters would be GenreId and ArtistId.

管道用于您尝试映射的新 POCO 的开始。因此SplitOn参数将是 GenreId 和 ArtistId。

Edit2: The problem is your POCO Album. You specify ArtistIdand GenreIdas properties but they basically belong to their respective POCO's.

Edit2:问题是你的 POCO Album。您将ArtistId和指定GenreId为属性,但它们基本上属于各自的POCO's.

public class Album
    {
        public virtual int AlbumId { 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; }
    }

and

var sql = @"SELECT AL.AlbumId
                 , AL.Title
                 , AL.Price
                 , AL.AlbumArtUrl
                 , GE.GenreId
                 , GE.Name
                 , GE.Description
                 , AR.ArtistId
                 , AR.Name 
            FROM Album AL 
      INNER JOIN Artist AR ON AR.ArtistId = AL.ArtistId 
      INNER JOIN Genre GE ON GE.GenreId = AL.GenreId";

using (var conn = connFactory.OpenConnection())
{
    var res = conn.Query<Album, Genre, Artist, Album>(sql, (album, genre, artist) =>
    {
        album.Genre = genre;
        album.Artist = artist;
        return album;
    }, splitOn: "GenreId,ArtistId");
}

should do the trick. You don't need GenreIdand ArtistIdanyway because you have a reference to those objects in Albums.

应该做的伎俩。您不需要GenreIdArtistId无论如何,因为您在Albums.

回答by ?smail Kocacan

I have faced same problem. Here is trick & example.

我遇到了同样的问题。这是技巧和示例。

public abstract class BaseEntity
{
    [Key]
    public int Id { get; set; }
}

public class Category : BaseEntity
{
    public string Name { get; set; }
}


public class Status : BaseEntity
{
    public string Name { get; set; }
}

public class User : BaseEntity
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public bool Active { get; set; }
}


public class TodoItem : BaseEntity
{
    public string Title { get; set; }
    public string Message { get; set; }
    public Status Status { get; set; }
    public Category Category { get; set; }
    public User User { get; set; }
    public DateTime CreatedOn { get; set; }
}

Using

使用

            string sql = @"select 
                          t.Id,
                          t.Title,
                          t.Message,
                          t.CreatedOn,
                          s.Id as Id,
                          s.Name,

                          c.Id as Id,
                          c.Name,

                          u.Id as Id,
                          u.Name,
                          u.Surname,
                          u.Active

                         from ToDoItem t
                        inner join [Status] s on (t.StatusId = s.Id)
                        inner join [Category] c on (t.CategoryId = c.Id)
                        inner join [User] u on (t.AssignUserId = u.Id)";
            var result = connection.Query<TodoItem, Status, Category, User, TodoItem>
                (sql, (todoItem, status, category, user) =>
            {
                todoItem.Status = status;
                todoItem.Category = category;
                todoItem.User = user;
                return todoItem;
            },splitOn: "Id,Id,Id,Id");

Here is trick splitOn: "Id,Id,Id,Id"

这是技巧splitOn: "Id,Id,Id,Id"