Table Inheritance in Entity Framework
- THP - Table Per Hierarchy
- TPT - Table Per Type
- TPC - Table Per Concrete Class
Table Per Hierarchy
Inheritance using TPH



Step 4: Remove discriminator columns from the base type Table "Department".
Step 5: Add Table mapping - right-click on Derived type entity and select "Table Mapping".
Step 6: Map Table and add a condition.


Step 7: Make a Base Entity abstract.

Step 8: Now update EDMX manually.
- Storage Model -In the storage Model, Define the Entity Type, Key, and properties that are the same as the database. There is no definition for derived in the storage model.
Coding
- <EntityType Name="Departments">
- <Key>
- <PropertyRef Name="DepartmentId" />
- </Key>
- <Property Name="DepartmentId" Type="int" Nullable="false" />
- <Property Name="DepartmentCode" Type="varchar" Nullable="false" MaxLength="25" />
- <Property Name="DepartmentName" Type="varchar" Nullable="false" MaxLength="100" />
- <Property Name="IsActive" Type="bit" Nullable="false" />
- <Property Name="DepartmentType" Type="int" Nullable="false" />
- </EntityType>
- Conceptual Model- In the conceptual Model, define the base entity without the discriminator column and also define a derived type.
Coding
- <EntityType Name="Department" Abstract="true">
- <Key>
- <PropertyRef Name="DepartmentId" />
- </Key>
- <Property Type="Int32" Name="DepartmentId" Nullable="false" />
- <Property Type="String" Name="DepartmentCode" Nullable="false" MaxLength="25" FixedLength="false" Unicode="false" />
- <Property Type="String" Name="DepartmentName" Nullable="false" MaxLength="100" FixedLength="false" Unicode="false" />
- <Property Type="Boolean" Name="IsActive" Nullable="false" />
- <NavigationProperty Name="Employees" Relationship="AdventureWorksModel.FK_Employees_Department" FromRole="Department" ToRole="Employee" />
- </EntityType>
- <EntityType Name="SalesDepartment" BaseType="AdventureWorksModel.Department">
- </EntityType>
-
Mapping Model- the Mapping Model contains the entity type mapping i.e. mapping between the conceptual model and storage model.ExampleIn this example, the mapping between the Department and Sales Department is defined in the same EntitySetMapping. The IsTypeOf keyword is used to indicate the type of entity set (i.e. base entity or derived entity). The Condition tag is a condition in which a sale Department is identified from the Department.
- <EntitySetMapping Name="Departments" >
- <EntityTypeMapping TypeName="IsTypeOf(AdventureWorksModel.Department)" >
- <MappingFragment StoreEntitySet="Departments" >
- <ScalarProperty Name="DepartmentId" ColumnName="DepartmentId" />
- <ScalarProperty Name="IsActive" ColumnName="IsActive" />
- <ScalarProperty Name="DepartmentName" ColumnName="DepartmentName" />
- <ScalarProperty Name="DepartmentCode" ColumnName="DepartmentCode" />
- </MappingFragment>
- </EntityTypeMapping>
- <EntityTypeMapping TypeName="IsTypeOf(AdventureWorksModel.SalesDepartment)">
- <MappingFragment StoreEntitySet="Departments">
- <ScalarProperty Name="DepartmentId" ColumnName="DepartmentId" />
- <Condition ColumnName="DepartmentType" Value="2" />
- </MappingFragment>
- </EntityTypeMapping>
- <EntityTypeMapping TypeName="IsTypeOf(AdventureWorksModel.AdminDepartment)">
- <MappingFragment StoreEntitySet="Departments">
- <ScalarProperty Name="DepartmentId" ColumnName="DepartmentId" />
- <Condition ColumnName="DepartmentType" Value="1" />
- </MappingFragment>
- </EntityTypeMapping>
- </EntitySetMapping>
In the next article, we will learn about TPT (Table per Type) and how to add it in EDMX.

Santosh YadavPosted Apr 12, 2013, 3:28 PM
Very nice article.
Jignesh TrivediPosted Jul 4, 2012, 8:43 AM
Hi Nishantha, It working fine for me and it ziped in winzip Version -15 Also you can try to testproject.zipx hope this help. Let me know if you find any issue.
Nishantha HettigodaPosted Jul 4, 2012, 6:19 AM
testProject.zip is Damage
Arjun PanwarPosted Jan 21, 2012, 11:17 PM
Thanks for good work