使用AutoMapper和多对一相关实体的类型为'System.StackOverflowException'的未处理异常

Termato

修复此错误后:当地图存在时,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>();

该错误执行以下一项操作(所有这三种操作均已发生并且很难重现特定的错误):

  1. 第一个是应用程序一直运行并最终超时。
  2. 程序“ [5256] iisexpress.exe”已退出,代码为-2147023895(0x800703e9)。
  3. 移至代码的另一部分(我的PagesViewModel)

(更新):

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:修正模型

  • EF不会生成视图模型,因此您应该能够更改它们以删除循环参考。
  • Ignore应该管用。我想再说一遍关于您为什么或在哪里遇到该错误的问题。我以前没看过,并且以前已经映射过类似的属性。

如果您可以发布完整的模型,查看模型和与数据库调用相关的代码,这将有所帮助。也许是一个模型,请查看模型以使其简短。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

VimService55.XmlSerializers.dll中发生类型为'System.StackOverflowException'的未处理异常

application.exe 中类型为“System.StackOverflowException”的未处理异常

Microsoft.WindowsAzure.Storage.dll中发生了类型为'System.StackOverflowException'的未处理异常

在application.exe中发生了'System.StackOverflowException'类型的未处理异常

生成报告会导致在system.windows.forms.dll中发生类型为'system.stackoverflowexception'的未处理异常

发生了类型为“System.StackOverflowException”的未处理异常,并且 toString 首先出现了一些不同的问题

类型为'System.StackOverflowException的未处理异常。由于当前线程处于堆栈溢出状态,因此无法求值表达式

类型System.Exception的异常未处理

发生类型为'System.NullReferenceException'的未处理异常

发生类型为'System.NullReferenceException'的未处理异常

发生类型为'System.FormatException'的未处理的异常

引发了类型为'System.StackOverflowException'的异常

如何处理类型为'System.ArgumentException'的未处理异常?

Windows 窗体“System.Data.dll 中发生类型为‘System.ArgumentException’的未处理异常”

MySql - System.Data.dll 中发生类型为“System.InvalidOperationException”的未处理异常

System.Data.dll 中发生类型为“System.AccessViolationException”的未处理异常

读取json文件异常“未处理的异常:System.TypeInitializationException:类型初始化程序”

在System.Drawing.dll中发生了'System.ArgumentException'类型的未处理异常,用于灰度

System.Data.dll中发生了'System.ArgumentException'类型的未处理异常

应用程序引发了未处理的异常。System.InvalidOperationException:无法>为类型解析服务

WebDriver.dll中发生类型为'System.InvalidOperationException'的未处理异常

错误:mscorlib.dll中发生了类型为'System.UnauthorizedAccessException'的未处理异常

未知模块中发生类型为“System.TypeInitializationException”的未处理异常

exe中发生类型为'System.IO.FileLoadException'的未处理异常

选择查询 C# 中发生类型为“System.Data.OleDb.OleDbException”的未处理异常

StackOverflowException未处理C#

未处理StackOverflowException HResult = -2147023895

mscorlib.dll中发生了'System.ComponentModel.Win32Exception'类型的未处理异常

BLL.dll中发生了'System.ArgumentException'类型的未处理异常