title: An unhandled exception of type 'System.StackOverflowException' using AutoMapper with many to one Related Entities
body: <p>After fixing this error: <a href="https://stackoverflow.com/questions/26062743/automapper-throws-missing-map-when-map-exists">AutoMapper throws Missing Map when Map exists</a> I received  the error:</p>
  
  <p><strong>Cannot evaluate expression because the current thread is in a stack overflow state.</strong>
  Here is where I initially get the error (As you can see, it's the same line as the first error):</p>
  
  <p><img src="https://i.stack.imgur.com/kmRRj.jpg" alt="enter image description here"></p>
  
  <p><strong>UPDATED:</strong>
  When I add the ignore statement to my <code>CreateMap&lt;&gt;();</code> declaration: </p>
  
  <pre><code>CreateMap&lt;Pages, PagesViewModel&gt;()
              .ForMember(m =&gt; m.PageTypes, x =&gt; x.Ignore())
              .ForMember(m =&gt; m.SiteMap, x =&gt; x.Ignore())
              .ForMember(m =&gt; m.Row, x =&gt; x.Ignore())
              .ForMember(m =&gt; m.Tracks, x =&gt; x.Ignore());
  </code></pre>
  
  <p>I received this message: <code>System.Reflection.TargetParameterCountException: Parameter count mismatch.</code> So i removed the Ignore statement and the CreateMaps are as follow:</p>
  
  <pre><code>CreateMap&lt;Pages, PagesViewModel&gt;();
  
  CreateMap&lt;PagesViewModel, Pages&gt;();
  </code></pre>
  
  <p>the error does one of the following (all three have happened and hard to reproduce a specific one):</p>
  
  <ol>
  <li>The first one is the application just keeps running and
  eventually times out.  </li>
  <li>The program '[5256] iisexpress.exe' has exited with code -2147023895 (0x800703e9). </li>
  <li>moves on to another part of the code (my PagesViewModel)</li>
  </ol>
  
  <p><strong>(UPDATED):</strong></p>
  
  <pre><code>private string PagesURL;
  [Required]
  [Display(Name = "Page URL")]
  [DataType(DataType.Url)]
  [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long and no longer than {1} characters.", MinimumLength = 2)]
  public string pagesURL
  {
      get { return PagesURL; }
      set { PagesURL = HttpUtility.UrlEncode(value); }
  }
  </code></pre>
  
  <p>When this third one comes up, I get the same error: <strong>Cannot evaluate expression because the current thread is in a stack overflow state.</strong></p>
  
  <p>I have tried separating out the code to see where and why it's doing this by trying the following: </p>
  
  <pre><code>List&lt;Pages&gt; getPages = db.Pages.ToList();
  List&lt;Pages&gt; getPagesOrdered = getPages.OrderBy(o =&gt; o.pageOrder).ToList();
  List&lt;PagesViewModel&gt; convertPages = new List&lt;PagesViewModel&gt;();
  foreach (Pages item in getPages)
  {
      PagesViewModel pagesViewModel = new PagesViewModel();
  
      pagesViewModel.pageDescription = item.pageDescription;
      //...loops through all other properties
  
      convertPages.Add(pagesViewModel);
  }
  </code></pre>
  
  <p>It gets all Pages from the database context, orders it and declared the <strong>PagesViewModel</strong> fine BUT when it goes inside the for loop, it declares the <strong>pagesViewModel</strong> variable fine and then right after it, before it even jumps to the <code>pagesViewModel.pageDescription = item.pageDescription;</code> line the application crashed <code>The program '[5176] iisexpress.exe' has exited with code 0 (0x0).</code></p>
  
  <p>MrChief, in the answer I linked at the top, suggested it is how my models connect with each other that is causing this issue. For example, <strong>PagesViewModel</strong> connects to <code>public virtual PageTypeViewModel PageTypes { get; set; }</code> and <strong>PageTypesViewModel</strong> connects back to <strong>PagesViewModel</strong> <code>public ICollection&lt;PagesViewModel&gt; Page { get; set; }</code></p>
  
  <p>My model requires those be connected and it works fine without auto mapper (with the exception of me trying to set each variable manually which cause the application to crash). I'm stuck and would appreciate some insight. </p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>After going here: <a href="http://www.symbolsource.org/Public/Wiki/Using" rel="nofollow noreferrer">http://www.symbolsource.org/Public/Wiki/Using</a> a co-worker of mine was able to get us the symbols for AutoMapper so we can trace into that source code. We added a trace breakpoint to output what was actually causing the issue. Here is what it output a few hundred times:</p>
  
  <p><strong>From "Pages"."PageTypes" to "PagesViewModel"."PageTypes"</strong></p>
  
  <p>So MrChief's prediction was correct. Now how to fix this, I don't know. </p>
  
  <p>This is where the PageViewModel links to the PageTypes view model: <code>public virtual PageTypeViewModel PageTypes { get; set; }</code> and then in PageTypesViewModel is links to PageViewModel: <code>public ICollection&lt;PagesViewModel&gt; Page { get; set; }</code></p>
  
  <p>This is how EF6 generated my model. I went about this doing model first. </p>
  
  <p>I might have forgotten to include some important information so let me know if there is anything missing. Thank you in advance for the help. </p>