Entity Framework - Relationship in Table per Type Inheritance

356 Views Asked by At

I have the following situation in my model:

public abstract class Person 
{
public int PersonId { get; set; }
public int FullName { get; set; }

public int LanguageId { get; set; }
}

public class Student : Person
{
public int PersonId { get; set; }
.
.//some data
.
}

public class Teacher : Person
{
public int PersonId { get; set; }
.
.//some data
.
}

public class Language 
{
public int LanguageId { get; set; }
public string Name { get; set; }
}

I used TPT approach for inheritance, and all run good. My problem: I want to add the Language Class, where a Person has a Language, and a Language has many Persons, so it's a one-to-many relationship. But in TPT approach Navigation Properties can't be inherited so I can not create the Language Object in Person class and neither the relationship in mapping class, I don't want to create a Navigation Property in Language Class because this table has a lot of relationships in my model, and I don't want to delete LanguageId in Person class 'cause it's a common property for all subclasses.

I've researched in several books, articles and here, but I've found nothing. In all examples explain the same situation (like here) but with the relationship in opposite direction.

I will appreciate your help, Thank you.

1

There are 1 best solutions below

0
On

As you say in TPT approach Navigation Properties can't be inherited.

One solution would be to add a relationship to the Language class at the Teacher and Student Level.

Another would be to move to a table per heirarchy approach.