ABP进行Map操作时指定对象Map时报错处理
ABP 中把Entity 的对象Map 到 Dto 内时,Entity 定义一个类型为long 的对象, 在 Dto 中有一个名称一样的对象,但类型是guid,
// StudentEntity
public class StudentEntity
{
public Guid RelatedStudentId
{
get; set;
}
}
// StudentDto
public class StudentDto
{
public Guid StudentId
{
get; set;
}
public Long RelatedStudentId
{
get; set;
}
}
以StudentEntity 和 StudentDto 为例,由于业务的变化,后端修改了数据库,暂时不动前端代码的情况下,
将Entity 内的RelatedPatientId 直接Map 给Dto 里的StudentId, 这样map 时会发生错误,提示 类型转换错误,无法将Guid 的数据赋值给long,
CreateMap<StudentEntity, StudentDto>()
.ForMember(d => d.StudentId, map => map.MapFrom(o => o.RelatedStudentId))
此时还是会将entity的Guid型RelatedPatientId map 给Dto 里的Long 型RelatedPatientId,
在进行map 操作时应避免这种数据的定义。