1

I'm new with AutoMapper and i'm using 5.1.1 version. I tried to map a list of model with a list of extended model. I get empty object in return.

My source model

public class spt_detail
{
    public string Name { get; set; }
    public string Age { get; set; }
}

My destination model

public class spt_detail_extended : spt_detail
{   
    public string Mm1 { get; set; }
    public string Mm2 { get; set; }
}

AutoMapper code

spt_detail details = db.spt_detail.ToList();

Mapper.Initialize(n => n.CreateMap<List<spt_detail>, List<spt_detail_extended>>());
List<spt_creance_detail_enrichi> cenr = AutoMapper.Mapper.Map<List<spt_detail>, List<spt_detail_enrichi>>(details);

Issue : details contains 8 rows but cenr 0.

Someone can helps me ?

Yannick
  • 13
  • 2

3 Answers3

0

Don't map the list, instead map like so:

Mapper.Initialize(n => n.CreateMap<spt_detail, spt_detail_extended>());

You call to do the map would stay the same:

List<spt_detail_extended> cenr = AutoMapper.Mapper.Map<List<spt_detail>, List<spt_detail_extended>>(details);
andyb952
  • 1,931
  • 11
  • 25
0

Mapping for spt_detail to spt_detail_extended.

You should create map rules only for models, like that:

Mapper.Initialize(cfg =>
{
    cfg.CreateMap<spt_detail, spt_detail_extended>();
});

And after that, you hould use the construction:

List<spt_detail_extended> extendeds = Mapper.Map<List<spt_detail_extended>>(details);

If you want to map other models, just add or edit your configuration.

PWND
  • 409
  • 3
  • 11
0

You have missed two steps in here.

The first one is that you need to initialize a list of your business object instead of initializing only a single one. You retrieve a list from your database (you use .Tolist())

here is a sample on how i initialized an object:

List <SptDetail> details = new List <SptDetail> {
 new SptDetail {
  Age = "10",
   Name = "Marion"
 },
 new SptDetail {
  Age = "11",
   Name = "Elisabeth"
 }
};

The second misstep is that you were mapping lits, i suggest work with your single business class objects instead like following:

Mapper.Initialize(n => n.CreateMap<SptDetail, SptDetailExtended>()
 .ForMember(obj => obj.ExProp1, obj => obj.MapFrom(src => src.Name))
 .ForMember(obj => obj.ExProp2, obj => obj.MapFrom(src => src.Age)));

And the key in the hole story is to use .ForMember to specify wich member goes where because the properties does not have same name.

here is a code sample that runs like a charm with:

internal class Program
{
    public static List<SptDetailExtended> InitializeExtendedObjects()
    {
        var details = new List<SptDetail>
        {
            new SptDetail
            {
                Age = "10",
                Name = "Marion"
            },
            new SptDetail
            {
                Age = "11",
                Name = "Elisabeth"
            }
        };
        //this is wrong db.spt_detail.ToList();

        Mapper.Initialize(n => n.CreateMap<SptDetail, SptDetailExtended>() 
          /*you need to use ForMember*/ .ForMember(obj => obj.ExProp1, obj => obj.MapFrom(src => src.Name))
                                        .ForMember(obj => obj.ExProp2, obj => obj.MapFrom(src => src.Age)));

        //instead of this Mapper.Initialize(n => n.CreateMap<List<spt_detail>, List<spt_detail_extended>>());

        //change your mapping like following too
        var cenr = Mapper.Map<List<SptDetailExtended>>(details);
        return cenr;
    }
    private static void Main(string[] args)
    {
        var result = InitializeExtendedObjects();
        foreach (var sptDetailExtended in result)
        {
            Console.WriteLine(sptDetailExtended.ExProp1);
            Console.WriteLine(sptDetailExtended.ExProp2);
        }
        Console.ReadLine();
    }
}

Hope this helps!

Haithem KAROUI
  • 1,533
  • 4
  • 18
  • 39