Charles Charles - 3 months ago 34
C# Question

EF Core HasDiscriminator with nullable discriminator

I'm trying to specify that I already have a discriminator on the entity, but am not sure how to represent it here.

The discriminator between them is whether or not a column is null. Obviously the case where it is null I can just put that explicitly, but what do I do for the case where its not?

protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Schedule>()
.HasDiscriminator<Guid?>("ManagerId")
.HasValue<EmployeeSchedule>(null)
.HasValue<ManagerSchedule>(/* What goes here? */);
}

Answer

Solved it by putting the property bool IsManagerSchedule on Schedule, and then using .HasValue<ManagerSchedule>(true).HasValue<EmployeeSchedule>(false);.