This problem will be explained around 3 classes: Account, IndividualAccount, and Doctor:
- First two classes are abstract
- IndividualAccount is Account's subclass
- Doctor is IndividualAccount's subclass
The first layer of inheritance (between Account and IndividualAccount) is being implemented with Table Per Type methodology. The second layer of inheritance (between IndividualAccount and Doctor) is being implamented with Table per Hierarchy methodology.
The Fluent API configurations is as following:
class AccountConfiguration : EntityTypeConfiguration<Account>
public AccountConfiguration()
{
HasKey(x => x.Id);
...
}
}
class IndividualAccountConfiguration : EntityTypeConfiguration<IndividualAccount>
{
public IndividualAccountConfiguration()
{
ToTable("IndividualAccounts");
...
}
}
class DoctorConfiguration : EntityTypeConfiguration<Doctor>
{
public DoctorConfiguration()
{
...
}
}
What we expect from this configuration is to have two tables: first one storing an Id and common properties to all users (e.g. username and password) and the second one storing properties common to all individuals (e.g. name and phone number). Moreover The second table will have a discriminator field distinguishing between Doctors and other type of Individuals we may have in our domain.
The problem arises when I try to get a Doctor by it's id. An exception will be thrown alleging that numerous columns are invalid, the most important one of them being:
Invalid column name 'Discriminator'.\r\n
To my surprise if I place [Table("IndividualAccounts")] above IndividualAccount class definition, problem will be resolved. But I have already set the table name in configuration (Fluent API). Why should I use the annotation in addition to the Fluent API?
Update
In presence of Annotation, Doctor's properties are placed in IndividualAccounts table which indeed is what we expected. But If I remove the annotation, a new migration will be created trying to move those fields to the base table Account!