0

I'm facing some problems working with EF DB First. Say I've got:

  • A table Person
  • A table Student with a foreign key pointing to Person
  • A table Teacher with a foreign key pointing to Person

DB structure

The model created from the database generates the next classes:

public class Person{
  this.Student= new HashSet<Student>();
  this.Teacher= new HashSet<Teacher>(); 
}

public class Student{}
public class Teacher{}

And what I'd really like to see is

public class Person{}
public class Student:Person{}
public class Teacher:Person{}

Is there any convention over configuration or anything I'm missing to get the inherited classes ?

UPDATE

Classes are generated in such a way because the model specifies these associations between Person, Teacher and Student. My question should be then...Is there any way to create a model from a DB using EF so that the model contains classes that inherit from other ones?

jobmo
  • 835
  • 1
  • 9
  • 19
  • show us the structure of your tables – Fabio Sep 11 '15 at 11:19
  • What do you mean DB First? If you do use a model, you can change the inheritance mapping there and the generated classes will be updated. If you reverse-engineered a database in a Code First project, you'll have to make the changes to mappings and classes by hand. EF can't guess whether the relation implies inheritance – Panagiotis Kanavos Sep 11 '15 at 11:28
  • I've described in more detail what the problem is. As @PanagiotisKanavos says, it's not an automated process – jobmo Sep 11 '15 at 13:28
  • 1
    @jobmo - *the programmer* modifies the model after reverse engineering. The classes will be regenerated after that. The model builder has no way of guessing whether a foreign key should be mapped as inheritance – Panagiotis Kanavos Sep 11 '15 at 13:30

1 Answers1

0

You're looking for Table-per-Type (TPT) mapping. Try the solution here:

Entity Framework DB-First, implement inheritance

Community
  • 1
  • 1
Tom
  • 796
  • 7
  • 7