这是我的域名实体
public class Email
{
[Key]
public int Id { get; set; }
public bool IsDefault { get; internal set; }
public string Name { get; set; }
}
public class Cause
{
[Key]
public int Id { get; set; }
public string Name { get; internal set; }
public string Email { get; set; }
}
DTO:
public class CauseDTO
{
public int Id { get; set; }
public string CauseName { get; internal set; }
public string CauseEmail { get; set; }
public string EmailName { get; set; }
public bool IsDefaultEmail { get; set; }
public int EmailId { get; set; }
}
映射配置文件:
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<Cause, CauseDTO>()
.ForMember(dest => dest.CauseEmail, map => map.MapFrom(src => src.Email))
.ForMember(dest => dest.CauseName, map => map.MapFrom(src => src.Name))
.ForMember(dest => dest.Id, map => map.MapFrom(src => src.Id))
.ForAllOtherMembers(x => x.UseDestinationValue());
CreateMap<Email, CauseDTO>()
.ForMember(dest => dest.EmailName, map => map.MapFrom(src => src.Name))
.ForMember(dest => dest.IsDefaultEmail, map => map.MapFrom(src => src.IsDefault))
.ForMember(dest => dest.EmailId, map => map.MapFrom(src => src.Id))
.ForAllOtherMembers(x => x.Ignore());
}
}
项目代码:
var causeDTO = ctx.Emails.Where(x => x.Id == 1).ProjectTo<CauseDTO>().FirstOrDefault();
causeDTO = ctx.Causes.Where(x => x.Id == 1).ProjectTo<CauseDTO>(causeDTO).FirstOrDefault();
在第一个查询中,我通过忽略所有其他对象得到了在Email和CauseDTO之间映射的causeDTO结果,在第二个查询中,我已经将同一个对象传递给原因和原因之间的MapDTO应用ForAllOtherMembers来使用目标值。但它不起作用。目标值不具有约束力,即EmailName,IsDefaultEmail,EmailId。
你能帮我解决一下这个问题吗?我需要在同一个实例上得到两个结果。