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.