1

How can I avoid creating a column as "Discriminator" inside the database , If I inherit my business class from model class ( model class is mapped on database table).

Because, at the moment, if I inherit my business class ( e.g Specifics) to an existing model class ( i.e DataSpecific ), It asks for code first migration. In the migration, I can see discriminator as new column. I really don't want this. Because, original model class is being used in the whole application and that code works fine.

How can I stop the creation of "descriminator" column

C# Code :

Model Class

public class DataSpecific
{

}

Busines Class

public class Specific
{

}

as a result I can see following code in the migration

 AddColumn("dbo.Consignments", "Discriminator", c => c.String(nullable: false, maxLength: 128));

How can I avoid this?

DocZerø
  • 8,037
  • 11
  • 38
  • 66
Usman
  • 2,742
  • 4
  • 44
  • 82
  • That's because you're using TPH (Table per Hierarchy). Opt for TPT or TPC and you'll avoid the `Discriminator` column. See https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application – haim770 Nov 08 '16 at 16:10
  • Is there any fluent api command or way by which we can instruct entity framerwork to use TPT or TPC? – Usman Nov 08 '16 at 16:14
  • Yes. That would be `ToTable("...")`. See https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines – haim770 Nov 08 '16 at 16:15

1 Answers1

1

Either apply NotMapped Data Annotation to your business class:

[NotMapped]
public class Specific : DataSpecific
{
}

or use Ignore Fluent API:

modelBuilder.Ignore<Specific>();
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343