4

I have 3 levels of nesting in my project. Something like this:

    public class Assignment
    {
        public Element Element { get; set; }
    }

    public class Element
    {
        public Subject Subject { get; set; }
    }

    public class Subject
    {
        public int? Id { get; set; }

        public string Subject_Title { get; set; }
    }

There are many other properties in each of the class. The database follows the same structure. Now I want to map assignment from database to view model.

The mapping I wrote using automapper works for the first time but not after that. So the value of Subject is null in subsequent runs while the value of Element is fine in all runs. Issue is with Subject only.

Can anyone point me to the right direction and tell me what am I doing wrong?

        Mapper.CreateMap<db_Subject, Subject>();

        Mapper.CreateMap<db_element, Element>()
              .ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.db_Subject));

        Mapper.CreateMap<db_assignment, Assignment>()
              .ForMember(dest => dest.Element, opt => opt.MapFrom(src => src.db_element));

Basically, db_subject is foreign key in db_element and similarly db_element is foreign key in db_assignment. The name of columns is different in some cases in view model.

Garima
  • 401
  • 5
  • 29
  • follow this http://stackoverflow.com/questions/13338262/automapper-and-flattening-nested-arrays/13338523#13338523 or this http://stackoverflow.com/questions/13348812/how-to-use-automapper-to-create-complex-viewmodel-objects-from-a-simple-object-a/13355873#13355873 – Prasad Kanaparthi Feb 16 '16 at 06:27
  • In your db_element, are you sure db_subject is not coming as null? If you're using EF, may be you need to explicitly load it. – freakyroach Feb 16 '16 at 06:34
  • @Prasad thanks for the links will check. – Garima Feb 16 '16 at 07:11
  • @freakyroach yes from DB subject is coming every time. – Garima Feb 16 '16 at 07:12
  • @Prasad it is still not working, the issue is above code is working fine for the first time and after that it stops working. Could you post some code here for more info. – Garima Feb 16 '16 at 07:19
  • Can you post the relevant portion of your code who actually use these Mappings? Also, which one is your view models? post all the relevant classes here. – jpgrassi Feb 18 '16 at 23:34
  • assuming all property names are same mapping from source -> destination object, you need only map the child objects sourceChild -> destinationChild and sourceGrandChild -> destinationGrandChild, and the automapper should handle the rest – Dave Alperovich Feb 22 '16 at 20:24
  • Agree @DaveAlperovich but somehow it didn't work. I put Mapper.Reset() and it started working, still not sure what the issue is though! – Garima Feb 23 '16 at 04:53

1 Answers1

1

Try this,

I'm presuming that your DB classes look like below.

public class DB_Assignment
{
    public DB_Element Db_Element { get; set; }
}

public class DB_Element
{
    public DB_Subject Db_Subject { get; set; }
}

public class DB_Subject
{
    public int? Db_Id { get; set; }
    public string Db_Subject_Title { get; set; }
}

Then create a mapping like this

Mapper.CreateMap<DB_Subject, Subject>()
    .ForMember(destination => destination.Id, options => options.MapFrom(source => source.Db_Id))
    .ForMember(destination => destination.Subject_Title, options => options.MapFrom(source => source.Db_Subject_Title))
    .ReverseMap();

Mapper.CreateMap<DB_Element, Element>()
    .ForMember(destination => destination.Subject, options => options.MapFrom(source => source.Db_Subject))
    .ReverseMap();

Mapper.CreateMap<DB_Assignment, Assignment>()
    .ForMember(destination => destination.Element, options => options.MapFrom(source => source.Db_Element))
    .ReverseMap();

Use like below

var db_Assignment_1 = new DB_Assignment { Db_Element = new DB_Element { Db_Subject = null } };
var assignment_1 = MappingEngine.Map<DB_Assignment, Assignment>(db_Assignment_1);

var db_Assignment_2 = new DB_Assignment { Db_Element = new DB_Element { Db_Subject = new DB_Subject { Db_Id = 1, Db_Subject_Title = "Some title" } } };
var assignment_2 = MappingEngine.Map<DB_Assignment, Assignment>(db_Assignment_2);

var db_Assignment_Lst = new List<DB_Assignment> { db_Assignment_1, db_Assignment_2 };
var assignment_Lst = MappingEngine.Map<List<DB_Assignment>, List<Assignment>>(db_Assignment_Lst);

Works fine for me.

Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62
  • Thanks for the code @Prasad but it is still the same issue. It works in first run and stops afterwards. I tried Mapper.Reset() before the mapping and then it started working. I am not sure what the issue is yet. – Garima Feb 18 '16 at 03:52