Usman Usman - 1 month ago 18
C# Question

How can 'discriminator' column be avoidable in Code First Entity Framework

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?

Answer

Either apply NotMapped Data Annotation to your business class:

[NotMapped]
public class Specific : DataSpecific
{
}

or use Ignore Fluent API:

modelBuilder.Ignore<Specific>();