4

コードをすばやく学習するために、次のチュートリアルに従っています。 Code First から新しいデータベースへ

チュートリアルのステップ 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 変数プロパティをチェックしようとすると、多くの場合、「関数の評価にはすべてのスレッドを実行する必要があります」というデバッグ エラーが表示されます。コードはローカル db にデータベースを作成し、値も挿入します。

プロパティのクラス Blog および Post の仮想指定子を削除すると、この問題を回避できます。

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

他のプロジェクトでも同じ例外に直面しています。多くのプロパティが「関数の評価にはすべてのスレッドを実行する必要があります」と表示されています</p>

私はEntity Framework 6.0で作業しています

編集:

ブログはDbC​​ontextオブジェクトのプロパティで、プログラムクラスのメイン関数で初期化しています。次に、ブログをコンテキストに追加してから保存しました。データベースを作成/更新し、指定した値を挿入しました。コードはリンク例と同じです。

【スタックトレース】 キャプチャしたスタックトレースです。

   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)
4

3 に答える 3

2

例外メッセージは、内にネストされたクラスとしてBlogおよびを使用したことを示していますが、指示には次のように記載されています。PostProgram

Program.cs の Program クラス定義の下に、次の 2 つのクラスを追加します。

個人的にはそれらを別々のファイルに入れたいのですが、それは別の問題です。それらが public クラスであることを考えると、それが失敗していることに私はまだ驚いていますが、それが問題のように思えます。

「関数の評価にはすべてのスレッドを実行する必要があります」-これは実際にはエラーではありませんが、プロパティを評価するためにさまざまなスレッドを使用する必要がある場合のデバッグの制限を示しているだけです。

于 2013-10-26T08:40:57.563 に答える
0

私にとっての問題は、リポジトリで を返していたが、返された結果でIQueryable<T>メソッドを使用することを Service メソッドで忘れていたことです。ToList()

于 2015-09-21T11:41:26.947 に答える