Reputation: 419
This problem will be explained around 3 classes: Account, IndividualAccount, and Doctor:
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!
Upvotes: 3
Views: 516
Reputation: 16983
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?
No the problem is not solved the problem now is hidden. Just check it out with SSM, IndividualAccounts table dose not contains Discriminator column!
ToTable("IndividualAccounts");
throws UnsupportedHybridInheritanceMapping exception, just enable all exceptions then you will get:
More info:
Multiple inheritance with Entity Framework TPC
I have modified my classes so that now I can see also the discriminator.
Next time please post your classes with the question. More info: How to create a Minimal, Complete, and Verifiable example
If you use the below line code, this will disable the convention exception but you will get another kind of problmes later:
modelBuilder.Conventions.Remove<MappingInheritedPropertiesSupportConvention>();
The problem EF try to create TPC and not TPT I will get contact with EF-Team and give you a feedback.
Upvotes: 1