2016-05-06 6 views
0

私はStateProvinceIDを入力し、これに関連する名前(Person.FirstName)を表示しようとしていますが、Visual StudioでAdventureWorks2014 microsoftデータベースを使用してテストを行っています州に関連する名前の取得(AdventureWorks2014)例外

static public void provincequery() 
    { 
     Console.WriteLine("Enter province ID"); 
     var theid = Convert.ToInt32(Console.ReadLine()); 
     using (var context = new Model1()) 
     { 
      var queryprovince = from test in context.StateProvince 
         where test.StateProvinceID == theid 
         select test; 
      foreach(var disp in queryprovince) 
      { 
       Console.WriteLine("\nProvince:"); 
       Console.WriteLine(disp.Name); 
       foreach(var test2 in disp.SalesTerritory.Customer) 
       { 
        Console.WriteLine(test2.Person.FirstName); 
       } 

      } 
     } 


    } 

しかし、これは私にエラーを返す保つ:StateProvinceID.Soまでのところ私はこれがやった「コマンド定義を実行中にエラーが発生した詳細については、内部例外を参照してください。」 内部例外:

 à System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) 
    à System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) 
    à System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6() 
    à System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) 
    à System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5() 
    à System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) 
    à System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) 
    à System.Data.Entity.Core.Objects.ObjectQuery`1.Execute(MergeOption mergeOption) 
    à System.Data.Entity.Core.Objects.DataClasses.EntityReference`1.Load(MergeOption mergeOption) 
    à System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad() 
    à System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject) 
    à System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__2(TProxy proxy, TItem item) 
    à System.Data.Entity.DynamicProxies.StateProvince_0A13F786927514ECF26BEB6F7007442E73C1FCAA3A743679986FA051D479F5AA.get_SalesTerritory() 
    à ConsoleApplication1.Program.requete2() dans c:\Users\julianp\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:ligne 152 
    à ConsoleApplication1.Program.Main(String[] args) dans c:\Users\julianp\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:ligne 33 
    à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    à System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    à System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    à System.Threading.ThreadHelper.ThreadStart() 

私はそのことworking.AdventureWorksスキーマがここに見つけることができる理由を本当に理解していけない:

https://moidulhassan.files.wordpress.com/2014/07/adventureworks2008_schema.gif

+0

まあ...内部の例外は何ですか? –

+0

どこが見えますか? – julian

+0

私は内側の例外を追加しました(私はそれが私の質問にあると思います) – julian

答えて

0

が解決ありがとう。 foreach(queryprovinceのvar disp)の最後にToList()を追加する必要がありました。

そのforeach(queryprovince.ToList()内のvar disp)

関連する問題