代码第一:避免使用标识符列并继承 [英] Code First: Avoid discriminator column and keep inheritance

查看:88
本文介绍了代码第一:避免使用标识符列并继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有:

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

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

Then I have to define 10+ POCO classes to define the tables in my database:

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

当然,因为 MyTable 继承自 BaseEntity 我得到了鉴别器字段。我想摆脱 Discriminator 字段,因为我不需要创建表 BaseEntity ,也不需要

Of course, because MyTable inherits from BaseEntity I get that Discriminator field. I want to get rid of the Discriminator field as I do not need the table BaseEntity to be created nor I need to implement some sort of inheritance into my database.

可以吗?

推荐答案

几个选项:


  • 使 BaseEntity 摘要

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

  • Make BaseEntity abstract
  • Use modelBuilder.Ignore<BaseEntity>() in yourDbContext.OnModelCreating

这篇关于代码第一:避免使用标识符列并继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆