Using AutoMapper to map the property of an object to a string

后端 未结 2 833
野性不改
野性不改 2020-12-05 12:53

I have the following model:

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I want to be able to

2条回答
  •  [愿得一人]
    2020-12-05 13:46

    ForMember means you are providing mapping for a member where you want a mapping between type. Instead, use this :

    Mapper.CreateMap().ConvertUsing();
    

    And Converter is

    public class TagToStringConverter : ITypeConverter
    {
        public string Convert(ResolutionContext context)
        {
            return (context.SourceValue as Tag).Name ?? string.Empty;
        }
    }
    

提交回复
热议问题