0

我的数据库中有 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”

堆栈跟踪 :

   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()
4

1 回答 1

0

无需在表 1 中放弃Include

但是当您将使用另一个表时,则需要它。例如

dbcontext.table2.Include("table1").Where(x => x.idTable2 == 1).FirstOrDefault();

实际上,在一对多关系中需要包含。

于 2013-04-29T08:49:47.983 回答