[ForeignKey("ReferenceDictionaryCategory,ReferenceDictionaryId")]
public virtual ReferenceDictionary ReferenceDictionary { get; set; }
在你尝试加载如下数据之后,
var test = await _dbcontext.FooEntities.Include(x => x.ReferenceDictionary).ToListAsync();
SELECT [x].[Id],
[x].[ReferenceDictionaryCategory],
[x].[ReferenceDictionaryId],
[x.ReferenceDictionary].[Category],
[x.ReferenceDictionary].[Id]
FROM [FooEntity] AS [x]
LEFT JOIN [ReferenceDictionary] AS [x.ReferenceDictionary]
ON ([x].[ReferenceDictionaryCategory] = [x.ReferenceDictionary].[Category])
AND ([x].[ReferenceDictionaryId] = [x.ReferenceDictionary].[Id])
这就是为什么你会
[ReferenceDictionaryCategory]
不是列。加上
[NotMapped]
因为这里不是叫专栏的地方。
有一个解决方法,您可以编写如下查询(如果你不想添加
ReferenceDictionaryCategory
列到
FooEntity
var result = from fe in _dbcontext.FooEntities
join rd in _dbcontext.ReferenceDictionaries
on new { Category = "FooCategory", fe.Id } equals new { rd.Category, rd.Id } into data
from final in data.DefaultIfEmpty()
select new FooEntity
{
Id = fe.Id,
ReferenceDictionaryId = fe.ReferenceDictionaryId,
ReferenceDictionary = final
};
这将生成这样的sql。
SELECT [rd].[Category], [rd].[Id], [fe].[Id] AS [Id0], [fe].[ReferenceDictionaryId]
FROM [FooEntity] AS [fe]
LEFT JOIN [ReferenceDictionary] AS [rd] ON (N'FooCategory' = [rd].[Category]) AND ([fe].[Id] = [rd].[