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

Ser*_*ero 5 c# automapper

我试图将从抽象基类继承的具有多对多关系的两个实体映射到也从其自己的抽象基类继承的 Dtos 中。当我只包含Essay类的映射时,除了Books集合为空之外,一切正常,一旦我添加该集合的映射,就会出现以下异常:

内部异常 1:ArgumentException:无法创建抽象类型 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)));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

在我的 Web API 项目中,我包含了AutoMapper.Extensions.Microsoft.DependendyInjection版本 7.0.0

Ser*_*ero 8

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