After fixing this error: AutoMapper throws Missing Map when Map exists I received the error:
Cannot evaluate expression because the current thread is in a stack overflow state. Here is where I initially get the error (As you can see, it's the same line as the first error):

UPDATED:
When I add the ignore statement to my CreateMap<>(); declaration:
CreateMap<Pages, PagesViewModel>()
.ForMember(m => m.PageTypes, x => x.Ignore())
.ForMember(m => m.SiteMap, x => x.Ignore())
.ForMember(m => m.Row, x => x.Ignore())
.ForMember(m => m.Tracks, x => x.Ignore());
I received this message: System.Reflection.TargetParameterCountException: Parameter count mismatch. So i removed the Ignore statement and the CreateMaps are as follow:
CreateMap<Pages, PagesViewModel>();
CreateMap<PagesViewModel, Pages>();
the error does one of the following (all three have happened and hard to reproduce a specific one):
(UPDATED):
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); }
}
When this third one comes up, I get the same error: Cannot evaluate expression because the current thread is in a stack overflow state.
I have tried separating out the code to see where and why it's doing this by trying the following:
List<Pages> getPages = db.Pages.ToList();
List<Pages> getPagesOrdered = getPages.OrderBy(o => o.pageOrder).ToList();
List<PagesViewModel> convertPages = new List<PagesViewModel>();
foreach (Pages item in getPages)
{
PagesViewModel pagesViewModel = new PagesViewModel();
pagesViewModel.pageDescription = item.pageDescription;
//...loops through all other properties
convertPages.Add(pagesViewModel);
}
It gets all Pages from the database context, orders it and declared the PagesViewModel fine BUT when it goes inside the for loop, it declares the pagesViewModel variable fine and then right after it, before it even jumps to the pagesViewModel.pageDescription = item.pageDescription; line the application crashed The program '[5176] iisexpress.exe' has exited with code 0 (0x0).
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, PagesViewModel connects to public virtual PageTypeViewModel PageTypes { get; set; } and PageTypesViewModel connects back to PagesViewModel public ICollection<PagesViewModel> Page { get; set; }
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.
UPDATE:
After going here: http://www.symbolsource.org/Public/Wiki/Using 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:
From "Pages"."PageTypes" to "PagesViewModel"."PageTypes"
So MrChief's prediction was correct. Now how to fix this, I don't know.
This is where the PageViewModel links to the PageTypes view model: public virtual PageTypeViewModel PageTypes { get; set; } and then in PageTypesViewModel is links to PageViewModel: public ICollection<PagesViewModel> Page { get; set; }
This is how EF6 generated my model. I went about this doing model first.
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.
I just tried with MaxDepth() method in Automapper and got resolve "StackOverflowException" Exception given by Automapper.
CreateMap<Pages, PagesViewModel>().MaxDepth(5);
just try with above code and get avoid the exception for a while.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With