不支持的映射异常-在Automapper中缺少类型映射配置



我肯定我错过了一些简单的东西。首先,我将展示为连接管道而编写的所有代码,然后展示异常消息。然后,我将列出我试图解决的问题。

LicenceTrackerProfile

public class LicenceTrackerProfile : Profile
{
    const string LicenceTrackerProfileName = "LicenceTrackerProfile";
    public override string ProfileName
    {
        get { return LicenceTrackerProfileName; }
    }
    protected override void Configure()
    {
        // initialize mappings here
        new ViewModelMappings(this).Initialize();
    }
}

MapperBootstrapper

public class MapperBootstrapper
{
    public void Configure()
    {
        var profile = new LicenceTrackerProfile();
        AutoMapper.Mapper.Initialize(p => p.AddProfile(profile));
    }
}

MappingBase

public abstract class MappingBase
{
    private readonly Profile _profile;
    protected MappingBase(Profile profile)
    {
        _profile = profile;
        _profile.SourceMemberNamingConvention = new PascalCaseNamingConvention();
        _profile.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
    }
    public Profile Profile
    {
        get { return _profile; }
    }
}

UniversalMapper

public class UniversalMapper : IUniversalMapper
{
    private readonly IMappingEngine _mappingEngine;
    public UniversalMapper(IMappingEngine mappingEngine)
    {
        _mappingEngine = mappingEngine;
    }
    public virtual TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
    {
        return _mappingEngine.Map(source, destination);
    }
}

ViewModelMappings

public class ViewModelMappings : MappingBase, IMappingInitializer
{
    private readonly Profile _profile;
    public ViewModelMappings(Profile profile) : base(profile)
    {
        _profile = profile;
        _profile.SourceMemberNamingConvention = new PascalCaseNamingConvention();
        _profile.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
    }
    public void Initialize()
    {
        //  data to domain mappings
        Profile.CreateMap<EFDTO.Enums.FileTypes, Domain.FileTypes>();
        Profile.CreateMap<EFDTO.Licence, Domain.Licence>();
        Profile.CreateMap<EFDTO.LicenceAllocation, Domain.LicenceAllocation>();
        Profile.CreateMap<EFDTO.Person, Domain.Person>();
        Profile.CreateMap<EFDTO.Software, Domain.Software>();
        Profile.CreateMap<EFDTO.SoftwareFile, Domain.SoftwareFile>();
        Profile.CreateMap<EFDTO.SoftwareType, Domain.SoftwareType>();
    }
}

注意,initialize方法和Configure方法正在被调用,所以它们不会被"错过"。

缺少类型映射配置或不支持映射。

映射类型:Software -> Software LicenceTracker.Entities.Software-> LicenceTracker.DomainEntities.Software

目标路径:Software

源值:LicenceTracker.Entities.Software

故障排除>忽略列。我计划忽略列,从所有列开始,然后逐个取消忽略,直到找到有问题的列。然而,令我惊讶的是,当我忽略所有列时,错误发生了:
Profile.CreateMap<EFDTO.Software, Domain.Software>()
    .ForMember(software => software.Licences, e => e.Ignore())
    .ForMember(software => software.Name, e => e.Ignore())
    .ForMember(software => software.SoftwareFiles, e => e.Ignore())
    .ForMember(software => software.Type, e => e.Ignore())
    .ForMember(software => software.Description, e => e.Ignore())
    .ForMember(software => software.Id, e => e.Ignore())
    .ForMember(software => software.TypeId, e => e.Ignore()
    .ForMember(software => software.ObjectState, e => e.Ignore());

域实体具有[DataContract](在类级别)和[DataMember](在方法级别)属性。我也将这些属性添加到EF实体中。

除此之外,我没有主意了。似乎都连接正确了。

我错过了什么?

我回来英勇地回答我的问题了。

问题出在创建UniversalMapper对象的服务中(请原谅这段草率的代码,它还不是最终的):

public class LicenceTrackerService : ILicenceTrackerService, IDisposable
{
    LicenceTrackerContext context = new LicenceTrackerContext();
    private MapperBootstrapper mapperBootstrapper;
    private IUniversalMapper mapper = new UniversalMapper(Mapper.Engine);
    private IUnitOfWork unitOfWork;
    public LicenceTrackerService()
    {            
        unitOfWork = new UnitOfWork(context, new RepositoryProvider(new RepositoryFactories()));
        mapperBootstrapper  = new MapperBootstrapper();
        mapperBootstrapper.Configure();
        Database.SetInitializer(new LicenceTrackerInitializer());
        context.Database.Initialize(true);
    }
    public int GetNumber()
    {
        return 42;
    }
    public List<LicenceTracker.DomainEntities.Software> GetSoftwareProducts()
    {
        var productsRepo = unitOfWork.Repository<Software>();
        var list = productsRepo.Query().Select().ToList();
        var softwareList = new List<LicenceTracker.DomainEntities.Software>();
        foreach (var software in list)
        {
            var softwareProduct = new LicenceTracker.DomainEntities.Software();
            softwareList.Add(Mapper.Map(software, softwareProduct));
        }
        return softwareList;
    }
    public void Dispose()
    {
        unitOfWork.Dispose();
    }
}

我仍然不确定为什么,但是在构造函数之外初始化映射器(默认值风格)是不高兴的。通过将该实例化移动到服务的构造函数中,它工作了:

    private IUniversalMapper mapper;
    public LicenceTrackerService()
    {
        mapper = new UniversalMapper(Mapper.Engine);
        ...
    }

显然有一些关于静态属性(Mapper.Engine)和默认实例化的东西我不理解。

无论如何,这没什么大不了的,因为我正计划将UniversalMapper注入到服务中。

编辑我现在真的找到问题所在了。它是有序的。对于Automapper,我必须在插入mapper之前用Profile初始化mapper。引擎到UniversalMapper。

显然是Mapper的Get方面。Engine属性不仅仅是对对象的内存引用。是的,快速浏览一下Automapper内部的代码就可以确认。

因此,将Get属性的结果赋值给UniversalMapper_mappingEngine字段必须发生在该引擎配置之后。

相关内容

  • 没有找到相关文章

最新更新