Entity Framework: How to avoid Discriminator column from table?

前端 未结 7 1827
无人及你
无人及你 2020-12-08 07:10

I have the following table created using Entity Framework Code First approach.

  1. How do I modify the C# code so that the unwanted Discriminato
相关标签:
7条回答
  • 2020-12-08 07:56

    Could also use Table per Type (TPT).

    http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt

    Table per Type (TPT)

    Table per Type is about representing inheritance relationships as relational foreign key associations. Every class/subclass that declares persistent properties—including abstract classes—has its own table. The table for subclasses contains columns only for each noninherited property (each property declared by the subclass itself) along with a primary key that is also a foreign key of the base class table.

    Implement TPT in EF Code First

    We can create a TPT mapping simply by placing Table attribute on the subclasses to specify the mapped table name (Table attribute is a new data annotation and has been added to System.ComponentModel.DataAnnotations namespace in CTP5.

    If you prefer fluent API, then you can create a TPT mapping by using ToTable() method:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BankAccount>().ToTable("BankAccounts");
        modelBuilder.Entity<CreditCard>().ToTable("CreditCards");
    }
    
    0 讨论(0)
提交回复
热议问题