【问题标题】:Prevent cyclic parent_ID `table per type` vs `table per hierarchy` vs `table per class`防止循环 parent_ID `table per type` vs `table per hierarchy` vs `table per class`
【发布时间】:2015-11-01 19:33:19
【问题描述】:

我在实体框架中使用 Table Per Hierarchy TPH,它基本上是扩展数据类型的公共核心属性的部分类。

我有 2 个对象/类; Course 对象和 Student 对象共享与 MyBaseCommonEntity 相同的基本属性。目标是能够对任一对象执行Service/ticket。我在建模时需要帮助,这是泛型类型还是继承类型?

我的问题:我使用 int 来引用 parentID。表的 pK Key 也是一个 int。

  1. 如何防止循环引用
  2. 设置时是否已完成此检查
  3. Hierarchy id 类型是否默认为我提供此功能?
  4. 每个类型的表与每个层次结构的表与每个类的表之间有什么区别`

    public partial class MyBaseCommonEntity
    {
        public int Id { get; set; }
        //Does it have any parents, does it belong to anyone
        public int? ParentId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    
        public System.DateTime CreatedDate { get; set; }
        public System.DateTime StartDate { get; set; }
        public System.DateTime? EndDate { get; set; }
    
        // Audit Stuff, whom, when why?
        public System.DateTime? AuditDate { get; set; }
        public string AuditUser { get; set; }
        public string AuditComments { get; set; }
    }
    

【问题讨论】:

    标签: entity-framework inheritance table-per-type table-per-hierarchy


    【解决方案1】:

    我认为您在这里混淆了两件事 - 层次结构支持(id/parentid)和类继承(重用一个基类)。前三点是关于等级的;第四个是关于继承的。

    1. 据我所知,EF 不直接支持您所指的样式中的循环引用检查。不过你可以自己做(在 SQL 服务器的情况下,例如通过使用 CTE 创建一个 CHECK 约束)

    2. 由于 EF 不支持这种形式的循环检查,所以根本不检查。

    3. EF 不直接支持 HierarchyID。你可以查看some third-party library,它增加了对hierarchyid的支持。

    4. 这点实际上不是关于父/子,而是关于如何在数据库中表示类继承。您提到的所有选项都在此处的相应知识库文章中进行了解释:Implementing Inheritance with the Entity Framework

    一般来说,让 Student 和 Course 从一件事继承似乎不是一个好主意;这些大多是无关的。您可以再输入一个条目,例如“AuditInfo”,然后在 Student 和 Course 类中添加对它的引用。可能更有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      相关资源
      最近更新 更多