.net-core - 实体框架核心导航属性和连接,正确的模式
问题描述
我正在努力了解在 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; }
}
这不仅适用于单元,还适用于许多其他通用查找项,例如仓库。有许多实体使用到仓库的链接。
使用这种查找时正确的方法是什么?
如果事情不清楚,我很抱歉,希望他们是。
解决方案
推荐阅读
- json - 使用 Svelte 和 Routiify POST 到没有服务器的本地 json 文件
- java - 如何使用 JAXB2 用动态元素编组 XML
- curl - 从 Bundesanzeiger 抓取数据
- python - 如何在字符串中转义 unicode 特殊字符并将其写入 UTF 编码文件
- javascript - 如何 ??-- 在回调中运行 createApi reducer
- angular - Angular 缓存加载过时的 index.html 文件
- phpunit - FilterIterator 应该是 PHPUnit\Framework\TestSuiteIterator 的实例
- google-apps-script - 进行排序时可以忽略工作表的第一行吗?
- ios - Flutter - 无法使用 CFBundleAlternateIcons 以编程方式更改应用程序图标
- selenium-ide - Katalon Recorder 将动态 ID 存储为变量并在下一个 URL 请求中使用它