Description:
Using MySql with Entity Framework 6 and doing a nested query such as:
var template = dbContext.Templates
.Include(t => t.Sections)
.Include(t => t.Sections.Select(s => s.Questions.Select(q => q.AnswerCertaintyOptions)))
.Include(t => t.Sections.Select(s => s.Questions.Select(q => q.AnswerOptions)))
.FirstOrDefault(template => template.TemplateId == 1);
Throws an exception. The error I get is:
"Unknown column 'Apply1.TemplateSectionId' in 'field list'"
Full Error details:
System.Data.Entity.Core.EntityCommandExecutionException was unhandled by user code
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.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
at pwc.vsat.api.WebApiApplication.TEMP_INIT_TEMPLATE() in C:\Dev\pwc.vsat\pwc.vsat.server\pwc.vsat.api\Global.asax.cs:line 40
at pwc.vsat.api.WebApiApplication.Application_Start() in C:\Dev\pwc.vsat\pwc.vsat.server\pwc.vsat.api\Global.asax.cs:line 31
InnerException:
ErrorCode=-2147467259
HResult=-2147467259
Message=Unknown column 'Apply1.TemplateSectionId' 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:
The generated SQL is:
SELECT
`Project4`.`TemplatePublicId`,
`Project4`.`TemplateId`,
`Project4`.`Name`,
`Project4`.`Description`,
`Project4`.`TemplateVersion`,
`Project4`.`Date`,
`Project4`.`C17` AS `C1`,
`Project4`.`TemplateSectionPublicId`,
`Project4`.`TemplateSectionId`,
`Project4`.`Title`,
`Project4`.`Description1`,
`Project4`.`TemplateId1`,
`Project4`.`IntroductionSteps`,
`Project4`.`C16` AS `C2`,
`Project4`.`C2` AS `C3`,
`Project4`.`C3` AS `C4`,
`Project4`.`C4` AS `C5`,
`Project4`.`C5` AS `C6`,
`Project4`.`C6` AS `C7`,
`Project4`.`C7` AS `C8`,
`Project4`.`C8` AS `C9`,
`Project4`.`C9` AS `C10`,
`Project4`.`C1` AS `C11`,
`Project4`.`C10` AS `C12`,
`Project4`.`C11` AS `C13`,
`Project4`.`C12` AS `C14`,
`Project4`.`C13` AS `C15`,
`Project4`.`C14` AS `C16`,
`Project4`.`C15` AS `C17`
FROM (SELECT
`Apply2`.`TemplateId`,
`Apply2`.`Name`,
`Apply2`.`Description`,
`Apply2`.`TemplatePublicId`,
`Apply2`.`TemplateVersion`,
`Apply2`.`Date`,
`Apply1`.`TemplateSectionId`,
`Apply1`.`Title`,
`Apply1`.`Description` AS `Description1`,
`Apply1`.`TemplateSectionPublicId`,
`Apply1`.`TemplateId` AS `TemplateId1`,
`Apply1`.`IntroductionSteps`,
`Apply1`.`C1`,
`Apply1`.`QuestionNumber` AS `C2`,
`Apply1`.`QuestionId` AS `C3`,
`Apply1`.`TemplateSectionId` AS `C4`,
`Apply1`.`QuestionNumber1` AS `C5`,
`Apply1`.`QuestionText` AS `C6`,
`Apply1`.`AssumptionText` AS `C7`,
`Apply1`.`InformationSource` AS `C8`,
`Apply1`.`Expertise` AS `C9`,
`Apply1`.`Id` AS `C10`,
`Apply1`.`TemplateQuestionId` AS `C11`,
`Apply1`.`Certainty` AS `C12`,
`Apply1`.`C2` AS `C13`,
`Apply1`.`C3` AS `C14`,
`Apply1`.`C4` AS `C15`,
CASE WHEN (`Apply1`.`TemplateSectionId` IS NULL) THEN (NULL) WHEN (`Apply1`.`QuestionNumber` IS NULL) THEN (NULL) ELSE (1) END AS `C16`,
CASE WHEN (`Apply1`.`TemplateSectionId` IS NULL) THEN (NULL) ELSE (1) END AS `C17`
FROM (SELECT
`Filter1`.`TemplateId`,
`Filter1`.`Name`,
`Filter1`.`Description`,
`Filter1`.`TemplatePublicId`,
`Filter1`.`TemplateVersion`,
`Filter1`.`Date`
FROM (SELECT
`Extent1`.`TemplateId`,
`Extent1`.`Name`,
`Extent1`.`Description`,
`Extent1`.`TemplatePublicId`,
`Extent1`.`TemplateVersion`,
`Extent1`.`Date`
FROM `Template` AS `Extent1`
WHERE 1 = `Extent1`.`TemplatePublicId`) AS `Filter1`) AS `Apply2`) AS `Project4`
ORDER BY
`Project4`.`TemplateId` ASC,
`Project4`.`C17` ASC,
`Project4`.`TemplateSectionId` ASC,
`Project4`.`C16` ASC,
`Project4`.`C3` ASC,
`Project4`.`C1` ASC
How to repeat:
Do nested includes as described above.
Suggested fix:
This seems to be similar to Bug #72004 - http://bugs.mysql.com/bug.php?id=72004
You can successfully retrieve data if you simplify the query to:
var template = dbContext.Templates
.Include(t => t.Sections)
.Include(t => t.Sections.Select(s => s.Questions.Select(q => q.AnswerCertaintyOptions)))
.FirstOrDefault(template => template.TemplateId == 1);
OR
var template = dbContext.Templates
.Include(t => t.Sections)
.Include(t => t.Sections.Select(s => s.Questions.Select(q => q.AnswerOptions)))
.FirstOrDefault(template => template.TemplateId == 1);
The issue seems to be with multiple nested queries at once.