I want that when retrieving the base class I should get all rows in the table disregarding the discriminator. I only want to use the discriminator when retrieving one of the derived classes. How can I accomplish that?
The first thing that I tried was to only set HasValue for the derived classes but not the base class like this...
modelBuilder.Entity<BaseClass>(entity =>
{
entity.HasDiscriminator(e => e.DiscriminatorProperty)
// .HasValue<BaseClass>()
.HasValue<DerivedClass1>(1)
.HasValue<DerivedClass2>(2);
});
...but that gives me the following error:
An exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'The entity type 'BaseClass' is part of a hierarchy, but does not have a discriminator value configured.'
Another thing that I tried was to configure the BaseClass for each possible discriminator value like this...
modelBuilder.Entity<BaseClass>(entity =>
{
entity.HasDiscriminator(e => e.DiscriminatorProperty)
.HasValue<BaseClass>(1)
.HasValue<BaseClass>(2)
.HasValue<DerivedClass1>(1)
.HasValue<DerivedClass2>(2);
});
...but when actually trying to retrieve the base class, I only got records with discriminator value of 1. EF Core seemed to only recognize the first configuration and ignore the next.
So my question is how can I configure the discriminator specifically for the derived classes but not for the base class? In other words when retrieving the base class I should get all of the records in the table.