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

本文关键字:映射 配置 不支持 类型 Automapper | 更新日期: 2023-09-27 17:54:42

我已经纠结了一天了,但是毫无效果。我是Automapper的新手,我正试图用viewModel映射EF域对象,但我收到以下异常:

Missing type map configuration or unsupported mapping.'r'n'r'nMapping types:'r'nCatalogueDefinitionFile -> CatalogueDefinitionFileViewModel'r'nDigital.PriceBuilder.Core.Domain.CatalogueDefinitionFile -> Digital.PriceBuilder.Web.Models.CatalogueDefinitionFileViewModel"}

目录定义文件的域POCO是:

public class CatalogueDefinitionFile : BaseEntity
{
    public CatalogueDefinitionFile()
    {
        this.ProductDefinitions = new List<ProductDefinition>();            
    }
    public string TargetApplication { get; set; }
    public virtual IList<ProductDefinition> ProductDefinitions { get; set; }
}

基本实体:

public abstract class BaseEntity
{
    public BaseEntity()
    {
        this.CreatedDate = DateTime.Now;
        this.UpdatedDate = DateTime.Now;
        this.IsActive = true;
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public DateTime UpdatedDate { get; set; }
    public string UpdatedBy { get; set; }
}

我已经创建了一个Profile:

public class DomainToViewModelMappingProfile : Profile
{
    public override string ProfileName
    {
        get
        {
            return "DomainToViewModelMappings";
        }
    }
    public DomainToViewModelMappingProfile()
    {
        ConfigureMappings();
    }

    /// <summary>
    /// Creates a mapping between source (Domain) and destination (ViewModel)
    /// </summary>
    private void ConfigureMappings()
    {
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<ProductDefinition, ProductDefinitionViewModel>().ReverseMap();
            cfg.CreateMap<CatalogueDefinitionFile, CatalogueDefinitionFileViewModel>().ReverseMap();
        });
        IMapper mapper = config.CreateMapper();
        mapper.Map<ProductDefinition, ProductDefinitionViewModel>(new ProductDefinition());
        mapper.Map<CatalogueDefinitionFile, CatalogueDefinitionFileViewModel>(new CatalogueDefinitionFile());

    }
}

Profile在AutoMapperConfiguration类中被引用,然后在Global.asax:

中被引用。
public class AutoMapperConfiguration
{
    public static void Configure()
    {
        // Create Automapper profiles
        Mapper.Initialize(m =>
        {
            m.AddProfile<DomainToViewModelMappingProfile>();
            m.AddProfile<ViewModelToDomainMappingProfile>();
        });
    }
}

viewModel看起来像这样:

public class CatalogueDefinitionFileViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string TargetApplication { get; set; }
    public bool IsActive { get; set; }
    public DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public DateTime UpdatedDate { get; set; }
    public string UpdatedBy { get; set; }
    public virtual IList<ProductDefinition> ProductDefinitions { get; set; }
}

然后在我的控制器中我有这个:

public ActionResult Index()
    {
        IEnumerable<CatalogueDefinitionFileViewModel> viewModel = null;
        IEnumerable<CatalogueDefinitionFile> files;
        files = _catalogueDefinitionFileService.GetCatalogueDefinitionFiles();
        viewModel = Mapper.Map<IEnumerable<CatalogueDefinitionFile>, IEnumerable<CatalogueDefinitionFileViewModel>>(files);
        return View(viewModel);
    }

抛出异常
viewModel = Mapper.Map<IEnumerable<CatalogueDefinitionFile>, IEnumerable<CatalogueDefinitionFileViewModel>>(files);

有人能帮我理解为什么会发生这种情况吗?

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

您的配置文件不做任何事情:

public class DomainToViewModelMappingProfile : Profile
{
// etc ...
    private void ConfigureMappings()
    {
        // You are just creating a local mapper config/instance here and then discarding it when it goes out of scope...
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<ProductDefinition, ProductDefinitionViewModel>().ReverseMap();
            cfg.CreateMap<CatalogueDefinitionFile, CatalogueDefinitionFileViewModel>().ReverseMap();    
        });
        // I assume this is just test code
        IMapper mapper = config.CreateMapper();    
        mapper.Map<ProductDefinition, ProductDefinitionViewModel>(new ProductDefinition());
        mapper.Map<CatalogueDefinitionFile, CatalogueDefinitionFileViewModel>(new CatalogueDefinitionFile());  
    }
}

试试这个:

public class DomainToViewModelMappingProfile : Profile
{
    public override string ProfileName
    {
        get
        {
            return "DomainToViewModelMappings";
        }
    }
    public DomainToViewModelMappingProfile()
    {
        ConfigureMappings();
    }

    /// <summary>
    /// Creates a mapping between source (Domain) and destination (ViewModel)
    /// </summary>
    private void ConfigureMappings()
    {
        CreateMap<ProductDefinition, ProductDefinitionViewModel>().ReverseMap();
        CreateMap<CatalogueDefinitionFile, CatalogueDefinitionFileViewModel>().ReverseMap();
    }
}

您正在继承的Profile类型可能与映射配置对象相关(因此具有相似/相同的本地方法)。

免责声明:我已经有一段时间没有使用Automapper了,但上述问题似乎是你的问题。

我刚刚测试过,一切正常。以下映射通过:

Mapper.CreateMap<CatalogueDefinitionFile, CatalogueDefinitionFileViewModel>();
var obj = Mapper.Map<IEnumerable<CatalogueDefinitionFileViewModel>>(new List<CatalogueDefinitionFile>{
    new CatalogueDefinitionFile
{
    Id = 101,
    Name = "test",
    TargetApplication = "test",
    IsActive = false,
    CreatedBy = "test",
    CreatedDate = DateTime.Now,
    UpdatedBy = "test",
    UpdatedDate = DateTime.Now,
    ProductDefinitions = new List<ProductDefinition> { new ProductDefinition { MyProperty = 100 } }}
});