EF Core从元数据获取反向导航属性

问题描述 投票:2回答:1

请考虑使用模型TodoItemPerson进行以下设置

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.ReviewerPerson.ReviewItem“反向相关”

我想我正在试图弄清楚如何访问modelBuilder.Hasxxx( ... ) and modelBuilder.Withxxx( ... )方法中的配置集。

我需要这个,因为我反思地遍历嵌套数据结构的查询结果集,并且需要确保我的算法具有前瞻性。

c# entity-framework-core system.reflection
1个回答
2
投票

EF Core元数据中的导航由INavigation接口表示。它们可以使用IEntityTypeGetNavigations延伸方法从FindNavigation获得。

一旦你有INavigation,逆向导航(如果存在)可以用FindInverse扩展方法获得。

您可以在我对Entity Framework Core 2.0.1 Eager Loading on all nested related entities的回答中看到示例导航遍历。

© www.soinside.com 2019 - 2024. All rights reserved.