我想使用 AutoMapper 导入数据。
我的目标类都继承自一个基类,该基类定义了一些源( )Entity
中不存在的属性CreatedOn, CreatedBy, ModifiedOn, ModifiedBy
假设我有一个源类 Unit:
public class UnitOld
{
public int Id { get; set; }
public string Name { get; set; }
}
这是我的目标类单元:
public class Unit : Entity
{
public Guid UnitId { get; set; }
public string UnitName { get; set; }
}
public class Entity
{
public string CreatedOn { get; set; }
public string CreatedBy { get; set; }
public string ModifiedOn { get; set; }
public string ModifiedBy { get; set; }
}
现在要创建我的映射,我必须编写:
Mapper.CreateMap<UnitOld, Unit>()
.ForMember(d => d.UnitName, o => o.MapFrom(s => s.Name))
.ForMember(d => d.UnitId , o => o.Ignore())
.ForMember(d => d.CreatedOn, o => o.Ignore())
.ForMember(d => d.CreatedBy, o => o.Ignore())
.ForMember(d => d.ModifiedOn, o => o.Ignore())
.ForMember(d => d.ModifiedBy, o => o.Ignore());
效果很好,问题是我有多个从实体继承的类,我不想重复自己。可以告诉 AutoMapperFor every class that interits from entity ignore the properties ...
吗?