none
Code First TPT Modeling where item can be multiple derived types RRS feed

  • Question

  • What is the best way to model the following kind of relationship in Code First?

    abstract Person

    Student: Person

    Teacher: Person

    Researcher: Person

    ...many more...

    Where a person is one OR MORE derived types.

    I like the idea of TPT but not sure that it is appropriate when the relationship is such that a person can be a student and a researcher.

    Tuesday, February 28, 2012 3:09 PM

Answers

  • Hi The Flower Guy,

    Welcome to MSDN Forum.

    Based on the issue, I think 'Student','Teacher' and 'Researcher' class have more differenct properties than same properties, with such a situation, I suggest you don't use inheritance, just create class one by one. If you use inheritance, you need to use TPH, not TPT, set a discriminator column to discriminate which entity this record belongs to. About how to create TPH in code frist, please refer to the link below.

    Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH)         

    This aritcle will help you with TPH in code first step by step, but I don't suggest you to use TPH with so many entities, because it will lead the 'Person' table in the database has a large number of columns with null value. Just imagine, you saved all the student records, teacher records and any other type of records in one table, is it make sense? It will affect the query performance certainly. If you want to use TPT, Look at the link below.

    Inheritance with EF Code First: Part 2 – Table per Type (TPT)                  

    I hope these can help.

    Best Regards         


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 1, 2012 6:19 AM
    Moderator