修复此错误后:当地图存在时,AutoMapper会抛出缺少的地图,我收到此错误:
无法计算表达式,因为当前线程处于堆栈溢出状态。这是我最初收到错误的地方(如您所见,它与第一个错误在同一行):
更新:当我将ignore语句添加到CreateMap<>();
声明中时:
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());
我收到此消息:System.Reflection.TargetParameterCountException: Parameter count mismatch.
所以我删除了Ignore语句,CreateMaps如下:
CreateMap<Pages, PagesViewModel>();
CreateMap<PagesViewModel, Pages>();
该错误执行以下一项操作(所有这三种操作均已发生并且很难重现特定的错误):
(更新):
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); }
}
当这第三个出现时,我会遇到相同的错误:无法计算表达式,因为当前线程处于堆栈溢出状态。
我尝试通过尝试以下操作来分离出代码,以查看执行此操作的位置和原因:
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);
}
当它进入for循环时,它从数据库上下文中获取所有Pages,对其进行排序并声明PagesViewModel很好,但它声明pagesViewModel很好,然后紧接着它,甚至跳到pagesViewModel.pageDescription = item.pageDescription;
应用程序崩溃的那一行。The program '[5176] iisexpress.exe' has exited with code 0 (0x0).
我在顶部链接的答案中,MrChief表示,这是我的模型之间如何相互连接的原因。例如,PagesViewModel连接到public virtual PageTypeViewModel PageTypes { get; set; }
,PageTypesViewModel连接回到PagesViewModel public ICollection<PagesViewModel> Page { get; set; }
我的模型要求将它们连接起来,并且在没有自动映射器的情况下也可以正常工作(除了我尝试手动设置每个变量外,这会导致应用程序崩溃)。我被困住了,不胜感激。
更新:
转到此处后:http : //www.symbolsource.org/Public/Wiki/使用我的一位同事能够为我们获取AutoMapper的符号,以便我们可以跟踪该源代码。我们添加了一个跟踪断点,以输出实际导致问题的原因。这是它输出数百次的结果:
从“页面”。“ PageTypes”到“ PagesViewModel”。“ PageTypes”
因此,基夫先生的预言是正确的。现在不知道该如何解决。
这是PageViewModel链接到PageTypes视图模型的地方:public virtual PageTypeViewModel PageTypes { get; set; }
然后在PageTypesViewModel中是指向PageViewModel的链接:public ICollection<PagesViewModel> Page { get; set; }
这就是EF6生成我的模型的方式。我首先去做这个模型。
我可能忘了包含一些重要信息,所以如果有什么遗漏,请告诉我。预先感谢您的帮助。
虽然我不太确定为什么代理创建会引起问题,但是我知道这有两种解决方法,它们不需要您关闭代理创建/延迟加载(至少不是永久用于全局作用域):
选项A:如果要使用投影:
// disable proxy creation just for the duration of query
_context.Configuration.ProxyCreationEnabled = false;
// do the query, map, go crazy!
// enable proxy again. or if your context goes out of scope after this call, then you can ignore re-enabling.
_context.Configuration.ProxyCreationEnabled = true;
选项B:如果您不使用投影
var entity = _context.Where(...); // or whatever query
// detach entity
_context.Entry(entity).State = EntityState.Detached;
// then call Map
现在,通过更新阅读,可能还有第三个选择:
选项C:修正模型
Ignore
应该管用。我想再说一遍关于您为什么或在哪里遇到该错误的问题。我以前没看过,并且以前已经映射过类似的属性。如果您可以发布完整的模型,查看模型和与数据库调用相关的代码,这将有所帮助。也许是一个模型,请查看模型以使其简短。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句