Link for all dot net and sql server video tutorial playlists
Link for slides, code samples and text version of the video
With single table inheritance one database table is used to store data for all of the entity types in the entire inheritance hierarchy. Let us understand this with an example.
In our organization we have 2 types of employees
1. Permanent Employees – AnuualSalary is specific for Permanent Employees
2. Contract Employees – HourlyPay & HoursWorked is specific for Contract Employees
By default LINQ to SQL creates one Employee entity class, based on Employees table
But from an application perspective we want 3 entity classes
Employee – This should be an abstract class and should contain all the common properties of permanent and contract employees. ID, Name & Gender properties should be present in this class.
PermanentEmployee – This class should inherit from the abstract Employee class and should contain AnnualSalary property
ContractEmployee – This class should inherit from the abstract Employee class and should contain HourlyPay & HoursWorked properties
To achieve this using the LINQ to SQL class designer
1. Right click on the designer surface and select Add – Class option. Change the class name to PermanentEmployee.
2. Cut AnnualSalary property from Employee class and paste it in PermanentEmployee class.
3. Right click on the designer surface and select Add – Class option. Change the class name to ContractEmployee.
4. Cut HourlyPay & HoursWorked properties from Employee class and paste them in ContractEmployee class.
5. Right click on PermanentEmployee and select Add – Inheritance option. In the “New Inheritance” window select “Employee” class from “Select a base class” dropdownlist and “PermanentEmployee” class from “Select a derived class” dropdownlist. This should create an inheritance relationship between Employee and PermanentEmployee classes.
6. Along the same lines, Right click on ContractEmployee and select Add – Inheritance option. In the “New Inheritance” window select “Employee” class from “Select a base class” dropdownlist and “ContractEmployee” class from “Select a derived class” dropdownlist. This should create an inheritance relationship between Employee and ContractEmployee classes.
7. Right click on the inheritance relationship arrow(that connects Employee and PermanentEmployee classes) and select “Properties”. Set the properties of the inheritance relationship.
8. Along the same lines, right click on the inheritance relationship arrow(that connects Employee and ContractEmployee classes) and select “Properties”. Set the properties of the inheritance relationship.
9. Finally right click on Employee class and select properties. In the properties window set Inheritance Modifier = abstract. This should make Employee class an abstract class.