【发布时间】:2018-09-30 17:23:31
【问题描述】:
我在 MySQL 中使用实体框架。假设我有以下实体:
-
Country-
State-
CityCarBuilding
-
-
要急切地包含一直到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();
它们都工作得很好。现在,我想将这两者结合起来,以便同时包含Cars 和Buildings,所以我执行以下操作:
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:
【问题讨论】:
-
(1) 您的查询没有问题 (2) 当然这是 MySQL 提供程序错误。报告为#78798,似乎没有人关心复制和修复它(3)解决方法......好吧,祝你好运。
-
@IvanStoev 谢谢你找到这个。我只是按照他们的要求为他们提供了一个完整的可重复测试用例。希望尽快解决。
-
为什么不通过 ThenInclude() 函数尝试使用 EF Core 进行多级包含。看看这个entityframework.net/include-multiple-levels
标签: c# mysql .net entity-framework entity-framework-6