获取与省(AdventureWorks2014)异常关联的名称

Getting name associated with province(AdventureWorks2014) exception

我正在使用 AdventureWorks2014 微软数据库和 visual studio(在 C# 中编码)进行测试。我正在尝试输入 StateProvinceID,然后显示与此关联的名称(Person.FirstName)StateProvinceID.So 到目前为止,我是这样做的:

 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);
                }

            }
        }


    }

然而这一直给我返回一个错误"An error occurred while executing the command definition. See the inner exception for details." 内部异常:

      à 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

谢谢

已解决;需要在 foreach 的末尾添加 ToList()(var disp in queryprovince)。

所以它的 foreach(var disp in queryprovince.ToList())