自动映射器:无法为集合创建抽象类型的实例

我正在尝试将两个具有多对多关系的实体映射为从抽象基类继承的Dto,Dtos也从其自己的抽象基类继承。当我仅包含 Essay 类的映射时,除了 Books 集合为null之外,其他所有东西都可以正常工作,在我为该集合添加映射后,我得到以下异常:

  

内部异常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)));
        }
    }
}

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

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

weedssjl 回答:自动映射器:无法为集合创建抽象类型的实例

当我根据帖子评论中的建议创建要点时,我意识到 Book dto的映射丢失了。不幸的是,关于该问题的例外情况尚不明确,这使我不得不在这里提出问题。添加映射后,一切都按预期开始工作。

本文链接:https://www.f2er.com/3134888.html

大家都在问