Introduction
Today, in this article let's play around with an interesting and very useful concept in entity framework.
Question: What is Table Per Type Hierarchy?
In simple terms "The parent entity is associated with many child entities where the child entities can inherit the data from the parent. Here there is just a table that is subdivided into multiple entities based upon the properties".
Step 1: Create a new web application

Step 2: Entities in DB looks like this:

Step 3: Adding New Entity Data Model Framework

Step 4: Select the tables from DB

Step 5: Entity in Model Browser

Step 6: Add New Entity





Step 7: Make base type of parent entity as abstract

Step 8: Delete the property from parent entity and put it in the new entity


Step 9: Delete the unnecessary properties from the parent entity but make sure to specify up in the conditions during mappings

Step 10: Do the table mappings for the child entities



Step 11: Make nullable properties to false whereever it's required


Step 12: The complete code of WebForm1.aspx looks like this:




Join the conversation! Your thoughts help the community grow.