简体   繁体   中英

error in automapper with child type

i'm having an error during automapper in win app in c# this row:

  config.CreateMap<T432, PROC>()
 .ForMember(d => d.TIPO.COD_TIPO , o => o.MapFrom(s => s.COD_TIPO))

the error is : Expression 'd => d.TIPO.COD_TIPO' must resolve to top-level member and not any child object's properties. Use a custom resolver on the child type or the AfterMap option instead.\\r\\nNome parametro: lambdaExpression"}

where

 class T432
 {
    public string COD_TIPO { get; set; }
 }
 class PROC 
 {
    public TIPO_PROC TIPO { get; set; }
 }
 class TIPO_PROC
 {
    public string COD_TIPO { get; set; }
 }

how is correct automapper?

thanks in advance

You need to use a custom type converter as AutoMapper cannot configure "nested" objects and only sets top level properties, take a look at the example on the automapper site https://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters

here is the example:

Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());

public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
{
    public DateTime Convert(string source)
    {
        return System.Convert.ToDateTime(source);
    }
}

This will map a string to a DateTime object. You can easily substitute your own types and provide any configuration within the Convert method.

Brother the correct thing is that Try This

config.CreateMap(T432,PROC)().ForMember(d=>d.TIPO, o => o.ResolveUsing(s => new TIPO {COD_TIPO=s.COD_TIPO}));

Sorry Use this "<>" instead of () in CreateMap, like CreateMap<>.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM