Mapping Nested Classes using Automapper
<p>I have 3 levels of nesting in my project. Something like this:</p>

<pre><code>    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; }
    }
</code></pre>

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

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

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

<pre><code>        Mapper.CreateMap&lt;db_Subject, Subject&gt;();

        Mapper.CreateMap&lt;db_element, Element&gt;()
              .ForMember(dest =&gt; dest.Subject, opt =&gt; opt.MapFrom(src =&gt; src.db_Subject));

        Mapper.CreateMap&lt;db_assignment, Assignment&gt;()
              .ForMember(dest =&gt; dest.Element, opt =&gt; opt.MapFrom(src =&gt; src.db_element));
</code></pre>

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