Part 12 - Single table inheritance - saving to database

In Part 11, we discussed creating a single table inheritance model and retrieving data. In this video we will discuss saving data to the database table using the single table inheritance model. We will continue with the example we worked with in Part 11.

Drag and drop a button control on the webform and set the following propertiesID="btnAddEmployees"Text="Add Employees"

Double click on the button control to generate the click event handler. Copy and paste the following code in the code-behind file.

protectedvoid btnAddEmployees_Click(object sender, EventArgs
e)

{

using (SampleDataContext dbContext = newSampleDataContext())

{

PermanentEmployee permanentEmployee = newPermanentEmployee

{

Name = "Emma",

Gender = "Female",

AnuualSalary = 65000

};

ContractEmployee contractEmployee = newContractEmployee

{

Name = "Kristie",

Gender = "Female",

HourlyPay = 50,

HoursWorked = 80

};

dbContext.Employees.InsertOnSubmit(permanentEmployee);

dbContext.Employees.InsertOnSubmit(contractEmployee);

dbContext.SubmitChanges();

}

}

Run the application and click "Add Employees" button

Please note:1. Since Employee is an abstract class, an instance of this class cannot be created.2. The Discriminator column in the database is updated depending on the type of the employee object (PermanentEmployee or ContractEmployee) being instantiated.