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.

8
  • follow this stackoverflow.com/questions/13338262/… or this stackoverflow.com/questions/13348812/… Commented Feb 16, 2016 at 6: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. Commented Feb 16, 2016 at 6:34
  • @Prasad thanks for the links will check. Commented Feb 16, 2016 at 7:11
  • @freakyroach yes from DB subject is coming every time. Commented Feb 16, 2016 at 7: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. Commented Feb 16, 2016 at 7:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.