C# 虚拟列表<;T>;:“;函数求值要求所有线程运行”;

C# 虚拟列表<;T>;:“;函数求值要求所有线程运行”;,c#,.net,entity-framework,debugging,visual-studio-2012,C#,.net,Entity Framework,Debugging,Visual Studio 2012,我遵循以下教程快速学习代码。 我已经完成了教程的第4步,当我尝试运行代码时,问题就出现了。我在以下代码中遇到以下异常 foreach (var item in query) { Console.WriteLine(item.Name); } {"Exception has been thrown by the target of an invocation."} 有一个内在的例外 {"Access is denied: 'CodeFas

我遵循以下教程快速学习代码。

我已经完成了教程的第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 variable属性时,其中很多都有调试错误,说“函数求值需要运行所有线程” 代码在本地数据库中创建数据库,并插入值

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

    public virtual List<Post> Posts { get; set; }
    public virtual Blog Blog { get; set; }
以下是异常(外部和内部)的堆栈跟踪


链接到的代码将写入目标实体框架5。在EntityFramework6中,虚拟代理对象使用
Lazy
实现延迟加载,为了线程安全,必须对其进行不同的处理。我的建议是在EF6中使用
虚拟ICollection
,而不是
虚拟列表
,或者向后拉到EF5,您正在参考的教程就是针对EF5设计的。

异常消息显示您已将
博客
帖子
用作
程序
中嵌套的类,鉴于说明书中规定:

在Program.cs中的程序类定义下面添加以下两个类

就我个人而言,我会把它们放在不同的文件中,但那是另一回事。考虑到它们是公共课程,我仍然对它的失败感到惊讶,但听起来这就是问题所在


至于“函数求值需要所有线程才能运行”-这实际上并不是一个错误,它只是显示了当求值属性需要使用不同线程时调试的限制。

我的问题是在我的存储库中,我正在返回一个
IQueryable
,但在我的服务方法中忘记对返回的结果使用
ToList()
方法。

您如何声明您的
博客类型?你能发布完整的堆栈跟踪吗?我已经用堆栈跟踪更新了这个问题。代码在我提供的示例链接中得到了充分的说明。这不是一个有用的堆栈跟踪-它只是通过外观显示调用
Environment.StackTrace
。异常应该已经包含了全部详细信息。@JonSkeet按照您的要求用外部和内部异常更新了帖子。外部异常System.Reflection.TargetInvocationException内部异常是System.TypeLoadException代码示例要求将
Blog
Post
放在
程序
声明的下方-看起来您已将其放在
程序
的声明中。我不知道这是否相关,但很有可能。问题在EF5和EF6中都存在。另外,更改为ICollection不会修复错误。好的,我有一个新问题,不确定是否应该为它创建一个新问题,只想让你们先运行它。我尝试使用(var db=new Context())创建DbContext的实例,问题是它创建了一次数据库,然后在我删除该数据库以再次测试它时,它不会重新创建该数据库。你知道我为什么会这样吗?编辑:nvm我刚刚调用了Database.CreateIfNotExists();这解决了我的问题。感谢大家的努力:)
   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)