Entity structure code first creates a discriminator column

I use the EF CF approach for a site with MySQL. For some reason, EF creates a column in my Post table called "Disccriminator" and contains the message "VARCHAR".

Why is this column created? Can I do something to avoid creating it? Are there any advantages to having this column?

+63
entity-framework
Sep 12 2018-11-11T00:
source share
2 answers

The Discriminator column is used and required in Scripting Table on Per-Hierarchy inheritance. If for example you have such a model ...

 public abstract class BaseEntity { public int Id { get; set; } //... } public class Post : BaseEntity { //... } public class OtherEntity : BaseEntity { //... } 

... and make BaseEntity part of the model, for example, by adding DbSet<BaseEntity> to your derived context, the Entity Framework will by default display this class hierarchy in one table, but present a special column - Discriminator - to distinguish between different types ( Post or OtherEntity ), stored in this table. This column is populated with the type name (again Post or OtherEntity ).

+103
Sep 12 '11 at 9:16 a.m.
source share

You can stop creating a column by adding [NotMapped] data annotation to models that inherit from your base class. This will tell EF not to add your class to future migrations by removing the discriminator column.

 public class BaseClass { } [NotMapped] public class InheritingClass : BaseClass { } 
+2
Apr 23 '18 at 9:29
source share



All Articles