Inheritance 自动映射:继承的映射不包括展平属性
我使用AutoMapper将包含各种值对象的实体映射到平面DTO 虽然我可以使用Inheritance 自动映射:继承的映射不包括展平属性,inheritance,automapper,Inheritance,Automapper,我使用AutoMapper将包含各种值对象的实体映射到平面DTO 虽然我可以使用.IncludeMembers()展开从实体映射到基本DTO,但当我尝试使用.IncludeBase为派生DTO添加映射时,它会抛出一个错误,因为它不会检测未显式映射的属性 下面是一些示例代码,说明了这个问题。(取消注释底部映射以查看错误): //实体 公共类客户 { public int Id{get;protected set;} 公共字符串名称{get;protected set;} 公共地址{get;prot
.IncludeMembers()
展开从实体映射到基本DTO,但当我尝试使用.IncludeBase
为派生DTO添加映射时,它会抛出一个错误,因为它不会检测未显式映射的属性
下面是一些示例代码,说明了这个问题。(取消注释底部映射以查看错误):
//实体
公共类客户
{
public int Id{get;protected set;}
公共字符串名称{get;protected set;}
公共地址{get;protected set;}
}
//价值对象
公共课堂演讲
{
公共字符串第1行{get;private set;}
公共字符串邮政编码{get;private set;}
}
//基Dto
公共类客户数据库
{
public int Id{get;protected set;}
公共字符串名称{get;protected set;}
公共字符串AddressLine1{get;protected set;}
公共字符串邮政编码{get;protected set;}
}
//派生DTO
公共类CreateCustomerTo:CustomerToBase
{
由{get;protected set;}创建的公共字符串
}
//派生DTO
公共类UpdateCustomerDto:CustomerDtoBase
{
由{get;protected set;}更新的公共字符串
}
公共静态类自动映射配置
{
公共静态IMapper配置()
{
var config=new-MapperConfiguration(cfg=>
{
AddProfile();
});
config.assertconfigurationsvalid();
返回config.CreateMapper();
}
}
内部类CustomProfile:Profile
{
公共自定义配置文件()
{
CreateMap()
.IncludeMembers(x=>x.Address)
.ForMember(m=>m.Id,o=>o.Ignore());
//这将使用默认约定映射邮政编码
CreateMap(MemberList.None)
.ForMember(m=>m.AddressLine1,o=>o.MapFrom(x=>x.Line1));
/*
//这会引发一个错误,即邮政编码尚未映射
CreateMap()
.IncludeBase()
.ForMember(m=>m.CreatedBy,o=>o.Ignore());
//这会引发一个错误,即邮政编码尚未映射
CreateMap()
.IncludeBase()
.ForMember(m=>m.UpdatedBy,o=>o.Ignore());
*/
}
}
void Main()
{
var mapper=AutoMapperConfiguration.Configure();
}
此映射是否存在不正确或缺失的内容?或者AutoMapper中是否存在缺陷/此功能不受支持
我可以通过显式地为嵌套对象映射添加所有映射来解决这个问题,例如
.ForMember(m=>m.Postcode,o=>o.MapFrom(x=>x.Postcode))
等。但是如果必须显式地映射每个属性,那么它就无法使用AutoMapper了。@lucianbagoanu感谢您的响应。如果您已经为此功能创建了一个请求,您是否可以将上面的内容作为答案发布?这样,人们将能够看到这是目前不受支持的功能,但将在x.x版中添加/修复,我可以将此问题标记为已回答。(您不能将评论标记为答案。)谢谢。实际上它刚刚合并。尝试构建。
// Entity
public class Customer
{
public int Id { get; protected set; }
public string Name { get; protected set;}
public Address Address { get; protected set; }
}
// Value object
public class Address
{
public string Line1 { get; private set; }
public string Postcode { get; private set; }
}
// Base Dto
public class CustomerDtoBase
{
public int Id { get; protected set; }
public string Name { get; protected set;}
public string AddressLine1 { get; protected set; }
public string Postcode { get; protected set;}
}
// Derived DTO
public class CreateCustomerDto : CustomerDtoBase
{
public string CreatedBy { get; protected set; }
}
// Derived DTO
public class UpdateCustomerDto : CustomerDtoBase
{
public string UpdatedBy { get; protected set; }
}
public static class AutoMapperConfiguration
{
public static IMapper Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<CustomProfile>();
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
}
internal class CustomProfile : Profile
{
public CustomProfile()
{
CreateMap<Customer, CustomerDtoBase>()
.IncludeMembers(x => x.Address)
.ForMember(m => m.Id, o => o.Ignore());
// This uses default convention to map Postcode
CreateMap<Address, CustomerDtoBase>(MemberList.None)
.ForMember(m => m.AddressLine1, o => o.MapFrom(x => x.Line1));
/*
// This throws an error that Postcode has not been mapped
CreateMap<Customer, CreateCustomerDto>()
.IncludeBase<Customer, CustomerDtoBase>()
.ForMember(m => m.CreatedBy, o => o.Ignore());
// This throws an error that Postcode has not been mapped
CreateMap<Customer, UpdateCustomerDto>()
.IncludeBase<Customer, CustomerDtoBase>()
.ForMember(m => m.UpdatedBy, o => o.Ignore());
*/
}
}
void Main()
{
var mapper = AutoMapperConfiguration.Configure();
}