【发布时间】:2016-11-09 20:48:47
【问题描述】:
这是我的问题,我正在尝试映射这两个实体,但我得到了一个异常:
发件人:
public int IdCorpoGestor { get; private set; }
public string Nome { get; private set; }
public string Email { get; private set; }
public string Federacao { get; private set; }
public DateTime DataIniMandato { get; private set; }
public DateTime DataFimMandato { get; private set; }
public string Telefone1 { get; private set; }
public string Telefone2 { get; private set; }
public int IdConselho { get; private set; }
[ForeignKey("IdConselho")]
public Conselho Conselho { get; private set; }
public int IdTipo { get; private set; }
[ForeignKey("IdTipo")]
public Indicador Tipo { get; private set; }
public bool Ativo { get; private set; }
}
收件人:
public class CorpoGestorDTO
{
public int IdCorpoGestor { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Federacao { get; set; }
public DateTime DataIniMandato { get; set; }
public DateTime DataFimMandato { get; set; }
public string Telefone1 { get; set; }
public string Telefone2 { get; set; }
public int IdConselho { get; set; }
public int IdTipo { get; set; }
public bool Ativo { get; set; }
public string Tipo { get; set; }
}
映射:
Mapper.Initialize(cfg => cfg.CreateMap<CorpoGestor, CorpoGestorDTO>()
.ForMember(x => x.Tipo, y => y.MapFrom(s => s.Tipo.Nome)));
从数据库结果调用 Mapper:
Mapper.Map<IEnumerable<CorpoGestor>, List<CorpoGestorDTO>>(result);
例外:
缺少类型映射配置或不支持的映射
编辑
在 AutoMapper 的 GitHub 上打开了一个问题,您可以在那里获得更多信息:Automapper 5.1.1 Can't map Complex object, aways invalid #1783
【问题讨论】:
-
这些属性的问题:Tipo, [ForeignKey("IdTipo")] public int IdTipo { get;私人套装; }, [ForeignKey("IdConselho")] public int IdConselho { get;私人套装; },公共 Conselho Conselho { 得到;私人套装; }
-
我已经指出了属性,你可以查看它们并修复它们
-
@viveknuna 我该怎么办?忽略?
-
修复数据类型,如果没有在To Class中定义则定义
-
@viveknuna 没听懂!定义什么和在哪里?
标签: c# asp.net-web-api2 automapper automapper-5