【问题标题】:How to disable inheritance relationship mapping entityframework core如何禁用继承关系映射实体框架核心
【发布时间】:2017-08-02 13:01:18
【问题描述】:
[Table("Rectangle")]
public partial class Rectangle
{
     int length;
   int breath;
}
[Table("Rhombus")]
public partial class Rhombus
{
    int length;
   int breath;
    int angle;
}

只是为了代码的可重用性,我需要这样

  [Table("Rhombus")]
   public partial class Rhombus:Rectangle
    {
        int angle;
    }

但它会因为 TPH 而创建鉴别器字段

那么如何禁用继承关系,以便我可以扩展实体只是为了代码可重用性或任何其他解决方法来实现这一点。

【问题讨论】:

  • 在使用 Table-per-Hierarchy 继承时,您无法删除鉴别器。不过,您可以对该列的外观进行一定程度的控制,如 here 所示。
  • 您可以将公共属性提取到接口并从该接口派生两种类型。目前,您无法在 CLR 模型的层次结构中包含两种类型的模型,但在数据库 TPH 中却没有。
  • ef core 确实没有保留任何使用继承进行任何类型分层的开口。您既不能扩展到或从 DBContext 中使用的任何类进行扩展

标签: inheritance .net-core entity-framework-core


【解决方案1】:
public class Shape
{
   int length;
   int breath;
}

[Table("Rectangle")]
public partial class Rectangle : Shape
{
}

[Table("Rhombus")]
public partial class Rhombus : Shape
{
    int angle;
}

【讨论】:

    【解决方案2】:

    考虑使用抽象类来禁用继承。

    正如@smit 提到的,您也可以改用界面

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多