Database Reference
In-Depth Information
Listing 2-21. Creating the Abstract Employee POCO Entity Class
[Table("Employee", Schema="Chapter2")]
public abstract class Employee
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int EmployeeId { get; protected set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Create a FullTimeEmployee POCO entity class that inherits from Employee using the code
in Listing 2-22.
3.
Listing 2-22. Creating the FullTimeEmployee POCO Entity Class
public class FullTimeEmployee : Employee
{
public decimal? Salary { get; set; }
}
Create an HourlyEmployee POCO entity class that inherits from Employee using the code
in Listing 2-23.
4.
Listing 2-23. Creating the HourlyEmployee POCO Entity Class
public class HourlyEmployee : Employee
{
public decimal? Wage { get; set; }
}
Add an auto-property of type DbSet<Employee> to your DbContext subclass.
5.
Override the OnModelCreating method of DbContext to map your concrete employee type
classes to the EmployeeType discriminator column, as shown in Listing 2-24.
6.
Listing 2-24. Overriding the OnModelCreating Method of DbContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Employee>()
.Map<FullTimeEmployee>(m => m.Requires("EmployeeType").HasValue(1))
.Map<HourlyEmployee>(m => m.Requires("EmployeeType").HasValue(2));
}
Note
nonshared properties (for example, salary and Wage) must have nullable types.
 
 
Search WWH ::




Custom Search