.net core 实体框架核心导航属性和连接,正确的模式

.net core 实体框架核心导航属性和连接,正确的模式,.net-core,entity-framework-core,.net Core,Entity Framework Core,我正在努力理解在EF Core中实现某些数据库关系的最佳方法 具体地说,它涉及导航属性,您可以在父对象中创建集合,在子对象中创建对象 看看这里的微软文档 有一个典型的父子关系用例 public class Course { public int CourseID { get; set; } public string Title { get; set; } public int Credits { get; set; } public int DepartmentID { ge

我正在努力理解在EF Core中实现某些数据库关系的最佳方法

具体地说,它涉及导航属性,您可以在父对象中创建集合,在子对象中创建对象

看看这里的微软文档

有一个典型的父子关系用例

public class Course
{
  public int CourseID { get; set; }
  public string Title { get; set; }
  public int Credits { get; set; }
  public int DepartmentID { get; set; }
  public virtual Department Department { get; set; }
}

public class Department
{
   public Department()
   {
     this.Courses = new HashSet<Course>();
   }  
   public int DepartmentID { get; set; }
   public string Name { get; set; }
   public decimal Budget { get; set; }
   public DateTime StartDate { get; set; }
   public virtual ICollection<Course> Courses { get; set; }
}
公共课
{
public int CourseID{get;set;}
公共字符串标题{get;set;}
公共整数积分{get;set;}
public int DepartmentID{get;set;}
公共虚拟部门部门{get;set;}
}
公共课系
{
公共部门()
{
this.Courses=newhashset();
}  
public int DepartmentID{get;set;}
公共字符串名称{get;set;}
公共十进制预算{get;set;}
公共日期时间起始日期{get;set;}
公共虚拟ICollection课程{get;set;}
}
现在,对于上面的用例来说,这很好

然而,我正在为工艺配方制定规范系统。这里有很多机器、材料、区域、建筑物和其他东西

数据库中的一个表格是“单位”,如测量单位——即千克、米、厘米等

此表用作数据库中许多表的查找,至少有20个

因此,如果我理解了推荐的方法,我最终会

pubic class Unit
{
  public Guid Id { get; set; }
  public string Name { get; set; }
  public virtual ICollection<Recipes> Recipes{ get; set; }
  public virtual ICollection<Coll2> Coll2{ get; set; }
  public virtual ICollection<Coll3> Coll2{ get; set; }
  public virtual ICollection<Coll4> Coll2{ get; set; }
  public virtual ICollection<Coll5> Coll2{ get; set; }
  public virtual ICollection<Coll..n> Coll..n{ get; set; }
}
公共类单元
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection配方{get;set;}
公共虚拟ICollection Coll2{get;set;}
公共虚拟ICollection Coll2{get;set;}
公共虚拟ICollection Coll2{get;set;}
公共虚拟ICollection Coll2{get;set;}
公共虚拟ICollection Coll..n{get;set;}
}
这不仅适用于单元,也适用于许多其他通用查找项,例如仓库。有许多实体使用到仓库的链接

使用这种查找时,正确的方法是什么


如果事情不清楚,我很抱歉,希望是这样。

规则很简单。你完全坐在驾驶座上。除了创建所需的导航属性之外,没有其他最佳实践。通常情况下,您不希望从查找导航到引用实体,因此请删除集合。好的,谢谢您这么快的回复。让我感到困惑的问题是如何以正确的方式加载查找数据。再次谢谢你,我不明白。也许你应该编辑你的问题并解释这方面。你不会理解的,我没有解释:),但谢谢。我一直致力于将一个大型应用程序从Django后端(我更熟悉这个世界)迁移到使用.NETCore。我非常喜欢代码优先EF方法的概念、标识系统以及如何轻松地分离关注点——特别是与Python提供的各种框架相比。问题是,似乎有很多方法可以实现同样的目标,但要知道什么是最好的是令人困惑的。文档很难理解,与简明相反,imho。您看到的文档是错误的(对于EF6)。EF核心对应的是。它包含了术语的定义和许多示例,其中包括,虽然它是针对集合导航属性的,但同样适用于多个方面的引用导航。