现在一直在努力争取这一天无济于事。我是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"}
CatalogueDefinitionFile的域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; }
}
我创建了个人资料:
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);
有人可以帮我理解为什么会这样吗?
提前致谢。
答案 0 :(得分:1)
您的个人资料无法执行任何操作:
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,但上述情况似乎是您的问题。
答案 1 :(得分:0)
我刚刚测试过,一切正常。以下映射传递 -
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 } }}
});