簡體   English   中英

使用多個Include()與嵌套的Select()時的EntityCommandExecutionException

[英]EntityCommandExecutionException when using multiple Include() with nested Select()

我正在使用MySQL的實體框架。 假設我有以下實體:

  • Country
    • State
      • City
        • Car
        • Building

為了熱切地包括Cars ,我可以使用以下內容:

context.Countries.
    Include(c => c.States.Select(s => s.Cities.Select(ci => ci.Cars))).ToList();

同樣,要包括一直到Buildings ,我可以使用:

context.Countries.
    Include(c => c.States.Select(s => s.Cities.Select(ci => ci.Buildings))).ToList();

他們都工作得很好。 現在,我想將這兩者結合起來,以包括CarsBuildings ,所以我做了以下幾點:

context.Countries.
    Include(c => c.States.Select(s => s.Cities.Select(ci => ci.Cars))).
    Include(c => c.States.Select(s => s.Cities.Select(ci => ci.Buildings))).ToList();

但每當我將兩者結合在一起- 使用上面的代碼 - 時 ,它會在內部異常中拋出一個EntityCommandExecutionException異常並帶有以下消息:

{“'字段列表'中的未知列'Apply1.Id'”}

我花了兩個小時試圖弄清楚查詢有什么問題,最后,我決定用SQL Server測試它,它沒有任何問題。

總結一下我的問題:

  • 知道為什么這不適用於MySQL嗎? 查詢本身有問題嗎?
  • 是否有任何解決方法/替代方案來實現這一點與MySQL?

請注意,這只發生在第三級( Select的第二級),例如,以下內容可以正常工作:

context.Countries.
    Include(c => c.States.Select(s => s.Cities.Select(ci => ci.Cars))).
    Include(c => c.States.Select(s => s.Laws.Select(l => l.PrivacyLaws))).ToList();

以下是相關的完整異常詳細信息:

System.Data.Entity.Core.EntityCommandExecutionException was unhandled
  HResult=-2146232004
  Message=An error occurred while executing the command definition. See the inner exception for details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
       at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
       at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
       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__DisplayClass7.<GetResults>b__5()
       at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.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.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at ConsoleApplication1.Program.Main(String[] args) in E:\Test\tmpEF\tmpEF\Program.cs:line 15
       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()
  InnerException: 
       ErrorCode=-2147467259
       HResult=-2147467259
       Message=Unknown column 'Apply1.Id' in 'field list'
       Number=1054
       Source=MySql.Data
       StackTrace:
            at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
            at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
            at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
            at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
            at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
            at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
            at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
            at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
            at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)
            at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
            at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
            at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
            at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
            at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
       InnerException: 

正如@IvanStoev所指出的那樣,已經是一個報告錯誤 ,但更好的問題是為什么你會調用產生笛卡爾生產的查詢。 從entityframework中包含多個數據集通常不是一個好主意。 IE

context.Countries.
    Include(c => c.States.Select(s => s.Cities.Select(ci => ci.Cars))).
    Include(c => c.States.Select(s => s.Cities.Select(ci => ci.Buildings))).ToList();

此查詢將撤回其中的數據集,每1個建築物中您擁有所有可能的汽車並包含城市信息。 你在浪費資源。 相反,你應該加載單獨的集合,這將避免錯誤。 並以最小的開銷加載您的相關實體。 最好你應該調用它,它也可以用作解決方法:

//You probably want to filter to the countries that have states
var countriesQuery = context.Countries.AsQueryable();
var statesQuery = countriesQuery.SelectMany(x => x.States);
statesQuery.Load();

var cityQuery = statesQuery.SelectMany(x => x.Cities);
cityQuery.Load();
cityQuery.SelectMany(x => x.Cars).Load();
cityQuery.SelectMany(x => x.Buildings).Load();
return countriesQuery.ToArray()

您可以決定在包含中的國家/地區加載說明狀態,但是您不應該堆疊多個嵌套,因為它的數量會增加

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM