c# - 虚拟列表<T> :“The function evaluation requires all threads to run”

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

我正在按照以下教程快速学习代码。 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; }

我的其他项目也面临同样的异常(exception)情况。许多属性显示“函数评估需要所有线程运行”

我正在使用 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)

最佳答案

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

Below the Program class definition in Program.cs add the following two classes.

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

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

关于c# - 虚拟列表<T> :“The function evaluation requires all threads to run”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19582199/

相关文章:

c# - 有效地将 IEnumerable 值折叠在一起

c# - 如何 : convert unicode character representation in string to the actual unicode character

c# - 带有表达式参数的过程

c# - 我可以创建一个用于属性参数的常量表达式吗?

c# - 什么时候在 DbContext 上执行查询

c# - Linq c# - 加入多个条件错误

javascript 属性 .net 属性

c# - 多重赋值(字段=属性=值)

.net - 从网络共享中读取文件会将其创建时间设置为其上次写入时间

c# - 处理通用接口(interface)时的解决方法