我正在使用带有Visual Studio的AdventureWorks2014微软数据库进行测试(在C#中编码)。我正在尝试输入StateProvinceID,然后显示与此StateProvinceID相关联的名称(Person.FirstName)。到目前为止,我这样做了: / p>
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);
}
}
}
}
但是这会让我一直回复“执行命令定义时出错。有关详细信息,请参阅内部异常。” 内部异常:
à 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()
我真的不明白为什么它不起作用.AdventureWorks架构可以在这里找到:
https://moidulhassan.files.wordpress.com/2014/07/adventureworks2008_schema.gif
谢谢
答案 0 :(得分:0)
解决;需要在foreach的末尾添加ToList()(queryprovince中的var disp)。
所以它的foreach(var disp in queryprovince.ToList())