请考虑使用模型TodoItem
和Person
进行以下设置
modelBuilder.Entity<TodoItem>()
.HasOne(t => t.Assignee)
// for simplicity lets assume a Person is assigned to only TodoItem
.WithOne(p => p.AssignedItem)
.HasForeignKey(t => t.AssigneeId);
modelBuilder.Entity<TodoItem>()
.HasOne(t => t.Reviewer)
.WithOne(p => p.ReviewItem)
// for simplicity lets assume a Person owns only one TodoItem
.HasForeignKey(t => t.ReviewerId);
反过来,可能使用Microsoft.EntityFrameworkCore.Metadata
,我怎么能弄明白呢
TodoItem.Assignee
(在HasOne( ... )
中配置)与“逆导航属性”Person.AssignedItem
相关联(在WithOne( ... )
中配置)TodoItem.Reviewer
与Person.ReviewItem
“反向相关”我想我正在试图弄清楚如何访问modelBuilder.Hasxxx( ... ) and modelBuilder.Withxxx( ... )
方法中的配置集。
我需要这个,因为我反思地遍历嵌套数据结构的查询结果集,并且需要确保我的算法具有前瞻性。
EF Core元数据中的导航由INavigation接口表示。它们可以使用IEntityType或GetNavigations延伸方法从FindNavigation获得。
一旦你有INavigation
,逆向导航(如果存在)可以用FindInverse扩展方法获得。
您可以在我对Entity Framework Core 2.0.1 Eager Loading on all nested related entities的回答中看到示例导航遍历。