I'm using Entity Framework 6.0 with Code First and Table per Hierarchy (TPH) inheritance.
See the following MWE (I omit the other fluent api statements to clarify where the problem is):
public abstract class Employee
{
public int EmployeeId { get; set; }
public string Type { get; set; }
}
public class CEO : Employee
{
public CEO() {Type = 1}
}
public class Other : Employee
{
}
public class InheritanceMappingContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>()
.Map<CEO>(m => m.Requires("Type").HasValue(1))
.Map<Other>(m => m.Requires("Type").HasValue(**<>1**));
}
}
I would like to map all entries which have a 1
for the column Type
(which should be the discriminator column) to the class CEO
. All entries with an entry different from 1
for the column Type
should be mapped to the class Other
.
Two issues arise here. The first one is how to write .HasValue(**<>1**));
as correct code. The second thing is that I want to use the Type
column in my code, but as this is the discriminator column I don't have access to it.