我的数据库中有4个表:
表1
idTable1
表2
idTable2
#idTable1
表3
idTable3
#idTable2
表4
idTable4
#idTable2
当我使用以下指令时,我有一个EntityCommandCompilationException:
dbcontext.table1.Include("table2.table3").Include("table2.table4").Where(x => x.idTable1 == 1).FirstOrDefault();
使用lambda表达式会产生相同的结果:
dbcontext.table1.Include(x => x.table2.Select(y => y.table3)).Include(x => x.table2.Select(y => y.table4)).Where(x => x.idTable1 == 1).FirstOrDefault();
我只尝试使用“Include(”table2.table3“)”然后只使用“Include(”table2.table4“)”并且它可以正常工作。
我是实体框架的初学者,我没有更多的想法来解决这个问题,希望你能帮助我。
此致
编辑:
异常消息:“准备命令定义时发生错误。有关详细信息,请参阅内部异常。”
InnerException:“不支持指定的方法”,有时只是“null”
StackTrace:
at System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree)
at System.Data.EntityClient.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree)
at System.Data.EntityClient.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Span span, ReadOnlyCollection`1 compiledQueryParameters, AliasGenerator aliasGenerator)
at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
at MyProjectServer_BLL.Implementation_EF.Table1_EF._getTable1(Int32 idTable1) dans c:\MyProject\MyProjectServer_BLL\Implementation_EF\Table1_EF.cs:ligne 43
at MyProjectServer_BLL.Base.Table1_Base.getTable1(Int32 idTable1) dans c:\MyProject\MyProjectServer_BLL\Base\Table1_Base.cs:ligne 45
at MyProject_ConsoleServer.Program.Main(String[] args) dans c:\MyProject\MyProject_ConsoleServer\Program.cs:ligne 62
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()
答案 0 :(得分:0)
无需在table1中提供Include
。
但是当您使用另一个表时,则需要它。例如
dbcontext.table2.Include("table1").Where(x => x.idTable2 == 1).FirstOrDefault();
实际上,在一对多的关系中需要包含。