Database Reference
In-Depth Information
Figure 6-10. The Employee table containing various types of employees
The Employee table contains hourly employees, salaried employees, and commissioned employees, which is a
subtype of salaried employees. To model this table with derived types for the hourly and salaried employees and a
commissioned employee type derived from the salaried employee, do the following:
1.
Create a new class in your project that inherits from DbContext.
2.
Create POCO entity classes for Employee, HourlyEmployee, SalariedEmployee,
and CommissionedEmployee, as shown in Listing 6-23.
Listing 6-23. Creating the Employee, HourlyEmployee, SalariedEmployee, and CommissionedEmployee
POCO Entities
public abstract class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
}
public class SalariedEmployee : Employee
{
public decimal? Salary { get; set; }
}
public class CommissionedEmployee : SalariedEmployee
{
public decimal? Commission { get; set; }
}
public class HourlyEmployee : Employee
{
public decimal? Rate { get; set; }
public decimal? Hours { get; set; }
}
Add an auto-property of type DbSet<Employee> to your DbContext subclass.
3.
Override the OnModelCreating method of DbContext to configure the TPH discriminator
values for each derived type, as shown in Listing 6-24.
4.
 
Search WWH ::




Custom Search