且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Code First Entity Framework中避免使用“ discriminator”列

更新时间:2023-12-01 14:13:28

要么应用 NotMapped 对您的业务类别的数据注释:

  [NotMapped] 
public class Specific:数据特定
{
}

或使用 忽略 Fluent API:

  modelBuilder.Ignore< Specific>(); 


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?

Either apply NotMapped Data Annotation to your business class:

[NotMapped]
public class Specific : DataSpecific
{
}

or use Ignore Fluent API:

modelBuilder.Ignore<Specific>();