Automapper:无法为集合创建抽象类型的实例

Automapper: Cannot create instance of abstract type for collections

我正在尝试将从抽象基础 class 继承的具有多对多关系的两个实体映射到也从它们自己的抽象基础 class 继承的 Dto。当我只包含 Essay class 的映射时,一切正常,除了 Books 集合为空,只要我添加该集合的映射我得到以下异常:

Inner Exception 1: ArgumentException: Cannot create an instance of abstract type Dtos.Dto`1[System.Int64].

考虑以下代码:

namespace Entities
{
    public abstract class Entity<TId> : Entity
        where TId : struct, IEquatable<TId>
    {
        protected Entity()
        {
        }

        public TId Id { get; set; }
    }

    public class Essay : Entity<long>
    {
        public string Name { get; set; }
        public string Author { get; set; }

        public List<EssayBook> EssayBooks { get; set; }
    }

    public class Book : Entity<long>
    {
        public string BookName { get; set; }
        public string Publisher { get; set; }
        public List<EssayBook> EssayBooks { get; set; }
    }

    public class EssayBook
    {
        public long EssayId { get; set; }
        public long BookId { get; set; }
        public Essay Essay { get; set; }
        public Book Book { get; set; }
    }
}

namespace Dtos
{
    public abstract class Dto<TId>
       where TId : struct, IEquatable<TId>
    {
        protected Dto()
        {
        }

        public TId Id { get; set; }
    }

    public sealed class Essay : Dto<long>
    {
        public string Name { get; set; }
        public string Author { get; set; }

        public List<Book> Books { get; set; }
    }

    public class Book : Dto<long>
    {
        public string BookName { get; set; }
        public string Publisher { get; set; }
    }
}

namespace DtoMapping
{
    internal sealed class EssayBookProfile : Profile
    {
        public EssayBookProfile()
        {
            this.CreateMap<Entities.Essay, Dtos.Essay>()
                .IncludeBase<Entities.Entity<long>, Dtos.Dto<long>>()
                .ForMember(dto => dto.Books, opt => opt.MapFrom(e => e.EssayBooks.Select(pl => pl.Book)));
        }
    }
}

我一直在寻找是否有不同的方法来配置此映射,但我总能找到这种方法。我还尝试专门为基础 classes 添加映射,但我得到了完全相同的结果。

在我的 Web API 项目中,我包含了 AutoMapper.Extensions.Microsoft.DependendyInjection7.0.0 版

当我根据 post 的评论中的建议创建要点时,我意识到 Book dto 的映射丢失了。不幸的是,异常并不清楚这个问题,这让我在这里问这个问题。添加映射后,一切都按预期开始工作。