这似乎非常类似于:EF4.1 multiple nested entity Includes gets NotSupportedException? 但异常消息不同,我们正在使用更新的库。
尝试加载具有3个级别的实体图表:Accounting Record包含多个AccountingOperation,其中每个AccountingOperation包含多个AccountingOperationLine。如果我想在第3级加载3个nevigation属性(1个综合帐户和2个分析帐户),则会出现以下例外情况,如下所示。
代码:
var v = dbEntities.Set<AccountingRecord>()
.Include(ar => ar.AccountingRecordOperations.Select(
aro => aro.AccountingRecordOperationLines.Select(arol => arol.AccountingSynthetic)))
.Include(ar => ar.AccountingRecordOperations.Select(
aro => aro.AccountingRecordOperationLines.Select(arol => arol.AccountingAnalytical1)))
.Include(ar => ar.AccountingRecordOperations.Select(
aro => aro.AccountingRecordOperationLines.Select(arol => arol.AccountingAnalytical2)))
.ToList();
异常
NotSupportedException: All objects in the EntitySet 'DbEntities.Entities' must have unique primary keys.
However, an instance of type '...AccountingSynthetic' and an instance of type '...AccountingRecordOperation' both have the same primary key value, 'EntitySet=Entities;ID=1104'
由于AccountingSynthetic和AccountingRecordOperation没有共享id,因此有些东西显然会混淆EF。
配置:
注意:
即使使用这样简单的查询,错误仍然存在:
var v = dbEntities.Set<AccountingRecord>()
.Include(ar => ar.AccountingRecordOperations.Select(aro => aro.AccountingRecordOperationLines.Select(arol => arol.AccountingSynthetic)));