【问题标题】:Code First: Avoid discriminator column and keep inheritance代码优先:避免鉴别器列并保持继承
【发布时间】:2015-06-17 13:57:30
【问题描述】:

在我的项目中,我有:

public class BaseEntity {
    [Key]
    public int Id {get; set; }
}

然后我必须定义 10+ POCO 类来定义我的数据库中的表:

public class MyTable : BaseEntity {
    //define properties here
}

当然,因为 MyTable 继承自 BaseEntity 我得到了 Discriminator 字段。我想去掉 Discriminator 字段,因为我不需要创建表 BaseEntity,也不需要在我的数据库中实现某种继承。

有可能吗?

【问题讨论】:

  • 我怀疑我错过了这里的重点,但为什么不直接放弃 BaseEntity 和继承模型呢?
  • @DavidArno 我试图在创建表键时保持某种一致性,我的所有表都将以 Id (int) 作为主键创建。是的,如果没有办法,我可以摆脱它......

标签: c# ef-code-first entity-framework-6


【解决方案1】:

几个选项:

  • 制作BaseEntityabstract
  • yourDbContext.OnModelCreating 中使用modelBuilder.Ignore<BaseEntity>()

【讨论】:

  • 我可以确认 •Make BaseEntity abstract 有效!其他方法没试过,也许别人可以确认或不确认
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多