首页 > 解决方案 > 实体框架核心导航属性和连接,正确的模式

问题描述

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

具体来说,它涉及导航属性,您可以在其中创建父级中的集合和子级中的对象。

在这里查看 MS 文档

关系、导航属性和外键

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

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; }
}

现在这对于上述用例来说很好。

但是,我正在研究工艺配方的规范系统。涉及许多机器、材料、区域、建筑物和其他事物。

数据库中的表之一是“单位”,如测量单位 - 即 kg、m、cm 等。

此表在数据库中的许多表中用作查找,至少 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; }
}

这不仅适用于单元,还适用于许多其他通用查找项,例如仓库。有许多实体使用到仓库的链接。

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

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

标签: .net-coreentity-framework-core

解决方案


推荐阅读