0

これが私の問題です。この両方のエンティティをマップしようとしていますが、例外が発生します。

から:

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

4

1 に答える 1

1

次のことを試してください。

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

あるデータ型を別のデータ型に変換する必要があります。そのために、マッピング構成に 2 行目が追加されます。

また、これは 1 回だけ呼び出す必要があります。複数回実行すると、以前の構成が上書きされます。

于 2016-11-09T21:32:36.520 に答える