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

前端 未结 2 856
抹茶落季
抹茶落季 2020-12-28 13:09

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 A         


        
2条回答
  •  感动是毒
    2020-12-28 13:38

    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
                    (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"

提交回复
热议问题