2016-11-09 55 views
0

这是我的问题,我想这两个实体映射无效,我跳投得到一个异常:Automapper 5.1.1不能映射复杂的对象,跳投

来源:

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.Map<IEnumerable<CorpoGestor>, List<CorpoGestorDTO>>(result); 

例外:

缺少类型映射配置或不支持的映射

EDIT

Openned在GitHub上的AutoMapper一个问题,你可以有更多的信息:Automapper 5.1.1 Can't map Complex object, aways invalid #1783

+0

这些属性的问题:Tipo,[ForeignKey(“IdTipo”)] public int IdTipo {get;私人设置; },[ForeignKey(“IdConselho”)] public int IdConselho {get;私人设置; },公共Conselho Conselho {get;私人设置; } –

+0

我已经指出了属性,你可以看看它们并修复 –

+0

@viveknuna我应该怎么做?忽视? – Fals

回答

1

尝试以下操作:

Mapper.Initialize(cfg => 
{ 
    cfg.CreateMap<CorpoGestor, CorpoGestorDTO>(); 
    cfg.CreateMap<Indicador, string>().ConvertUsing(x=> x.Nome); 
} 

您需要一种数据类型转换为另一种。为此,第二行添加到您的映射配置中。

另外,您应该只调用此一次。多次执行会覆盖以前的配置。

+0

@Fals正在使用.ForMember(x => x.Tipo,y => y.MapFrom(s => s.Tipo.Nome)),所以问题不在这个字段。 –

+0

@Amy不工作!同样的问题仍然存在,即使这种修改! – Fals

+0

不起作用不会告诉我们任何有用的东西。我们需要更多的信息。 – Amy