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?

4
  • show us the structure of your tables Commented Sep 11, 2015 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 Commented Sep 11, 2015 at 11:28
  • I've described in more detail what the problem is. As @PanagiotisKanavos says, it's not an automated process Commented Sep 11, 2015 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 Commented Sep 11, 2015 at 13:30

1 Answer 1

0

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

Entity Framework DB-First, implement inheritance

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.