【问题标题】:virtual List<T> :“The function evaluation requires all threads to run”virtual List<T> :“函数求值需要所有线程运行”
【发布时间】:2013-11-04 02:53:43
【问题描述】:

我正在按照以下教程快速学习代码。 Code First to a New Database

我已经完成了教程的第 4 步:当我尝试运行我的代码时出现了问题。我在以下代码中遇到以下异常...

    foreach (var item in query)
    {
        Console.WriteLine(item.Name);
    }
    {"Exception has been thrown by the target of an invocation."}

有一个内部异常

    {"Access is denied: 'CodeFastNewDatabase.Program+Blog'.":""}

当我尝试检查 db 变量属性时,其中很多都有调试错误说“函数评估需要所有线程运行” 该代码正在我的本地数据库中创建数据库,同时插入值。

如果我删除类 Blog 和 Post 中属性的虚拟说明符,我可以解决这个问题

    public virtual List<Post> Posts { get; set; }
    public virtual Blog Blog { get; set; }

我的其他项目也面临同样的例外情况。很多属性都显示“函数求值需要所有线程运行”

我正在使用 Entity Framework 6.0

编辑:

Blog 是DbContext 对象的一个​​属性,我已经在程序类的main 函数中初始化了。然后在上下文中添加博客,然后保存它。那创建/更新了数据库并插入了我的给定值。代码与示例链接中的相同。

[堆栈跟踪] 这是我捕获的堆栈跟踪。

   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at CodeFastNewDatabase.Program.Main(String[] args) in f:\Dropbox\Projects\visual studio 2012\Projects\CodeFastNewDatabase\CodeFastNewDatabase\Program.cs:line 32
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

这是两个异常(外部和内部)的堆栈跟踪

    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
    at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap(Translator translator, Type elementType, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer)
    at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator)
    at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
    at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__a()
    at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
    at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9()
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
    at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
    at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
    at System.Lazy`1.CreateValue()
    at System.Lazy`1.LazyInitValue()
    at System.Lazy`1.get_Value()
    at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
    at CodeFastNewDatabase.Program.Main(String[] args) in f:\Dropbox\Projects\visual studio 2012\Projects\CodeFastNewDatabase\CodeFastNewDatabase\Program.cs:line 33
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()

内部异常堆栈跟踪

    at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
    at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
    at System.Reflection.Emit.TypeBuilder.CreateType()
    at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.ProxyTypeBuilder.CreateType(ModuleBuilder moduleBuilder)
    at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.BuildType(ModuleBuilder moduleBuilder, ClrEntityType ospaceEntityType, MetadataWorkspace workspace)
    at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.TryCreateProxyType(EntityType ospaceEntityType, MetadataWorkspace workspace)
    at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.GetProxyType(ClrEntityType ospaceEntityType, MetadataWorkspace workspace)
    at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(EntityColumnMap columnMap, TranslatorArg arg)
    at System.Data.Entity.Core.Query.InternalTrees.EntityColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
    at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue)
    at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg)
    at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(SimpleCollectionColumnMap columnMap, TranslatorArg arg)
    at System.Data.Entity.Core.Query.InternalTrees.SimpleCollectionColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
    at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap[T](ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer)

【问题讨论】:

  • 你是如何声明你的Blog类型的?你能发布完整的堆栈跟踪吗?
  • 我已经用堆栈跟踪更新了这个问题。并且代码在我提供的示例链接中得到了充分说明。 msdn.microsoft.com/en-us/data/jj193542.aspx
  • 这不是一个有用的堆栈跟踪 - 它只是显示你调用Environment.StackTrace,从它的外观。异常应该已经包含完整的详细信息。
  • @JonSkeet 按照您的要求用外部和内部异常更新了帖子。外部异常 System.Reflection.TargetInvocationException 内部异常是 System.TypeLoadException
  • 代码示例说将BlogPost 低于 Program 声明 - 看起来你已经把它放在 Program 的声明。我不知道这是否相关,但很可能。

标签: c# .net entity-framework debugging visual-studio-2012


【解决方案1】:

您链接到的代码是针对 Entity Framework 5 编写的。在 Entity Framework 6 中,虚拟代理对象使用 Lazy&lt;T&gt; 的实现进行延迟加载,并且必须以不同方式处理它以保证线程安全。我的建议是在 EF6 中使用 virtual ICollection&lt;Post&gt; 而不是 virtual List&lt;Post&gt;,或者退回到您所引用的教程所针对的 EF5。

【讨论】:

  • 在 EF5 和 EF6 中问题仍然存在。更改为 ICollection 也无法修复错误。
【解决方案2】:

异常消息显示您已将BlogPost 用作嵌套在Program 中的类,而说明指出:

在 Program.cs 中的 Program 类定义下方添加以下两个类。

我个人会将它们放入单独的文件中,但那是另一回事。鉴于它们是公共课程,我仍然对它失败感到惊讶,但听起来这就是问题所在。

至于“函数评估需要所有线程都运行”——这实际上不是一个错误,它只是显示了在评估一个属性需要使用各种线程时调试的局限性。

【讨论】:

  • 好吧,我有一个新问题,不知道我是否应该为它创建一个新问题,只是想先由你们运行它。我尝试使用 using(var db = new Context()) 创建 DbContext 的实例,问题是它创建了一次数据库,然后当我删除数据库以再次对其进行测试时,它不会重新创建数据库。知道我为什么会这样吗?编辑:nvm 我刚刚调用了 Database.CreateIfNotExists();这解决了我的问题。感谢大家的努力:)
【解决方案3】:

我的问题是,在我的存储库中,我返回了一个 IQueryable&lt;T&gt;,但忘记在我的 Service 方法中对返回的结果使用 ToList() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 2020-08-19
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多