Walkthrough: Creating LINQ to SQL Classes by Using Single-Table Inheritance (O/R Designer)
The Object Relational Designer (O/R Designer) supports single-table inheritance as it is typically implemented in relational systems. This walkthrough expands upon the generic steps provided in the How to: Configure Inheritance by Using the O/R Designer topic and provides some real data to demonstrate the use of inheritance in the O/R Designer.
During this walkthrough, you will perform the following tasks:
Create a database table and add data to it.
Create a Windows Forms application.
Add a LINQ to SQL file to a project.
Create new entity classes.
Configure the entity classes to use inheritance.
Query the inherited class.
Display the data on a Windows Form.
To see how inheritance works, you will create a small Person table, use it as a base class, and then create an Employee object that inherits from it.
To create a base table to demonstrate inheritance
In Server Explorer/Database Explorer, right-click the Tables node and click Add New Table.
You can use the Northwind database or any other database that you can add a table to.
In the Table Designer, add the following columns to the table:
Set the ID column as the primary key.
Save the table and name it Person.
So that you can verify that inheritance is configured correctly, the table needs some data for each class in the single-table inheritance.
To add data to the table
Open the table in data view. (Right-click the Person table in Server Explorer/Database Explorer and click Show Table Data.)
Copy the following data into the table. (You can copy it and then paste it into the table by selecting the whole row in the Results Pane.)
Now that you have created the table, create a new project to demonstrate configuring inheritance.
To create the new Windows Application
From the File menu, create a new project.
Name the project InheritanceWalkthrough.
The O/R Designer is supported in Visual Basic and C# projects. Create the new project in one of these languages.
Click the Windows Forms Application template and then click OK. For more information, see Creating Windows-Based Applications.
The InheritanceWalkthrough project is created and added to Solution Explorer.
Configure the inheritance by dragging an Inheritance object from the Toolbox onto the design surface.
To create the inheritance
In Server Explorer/Database Explorer, navigate to the Person table that you created earlier.
Drag the Person table onto the O/R Designer design surface.
Drag a second Person table onto the O/R Designer and change its name to Employee.
Delete the Manager property from the Person object.
Delete the Type, ID, FirstName, and LastName properties from the Employee object. (In other words, delete all properties except for Manager.)
From the Object Relational Designer tab of the Toolbox, create an Inheritance between the Person and Employee objects. To do this, click the Inheritance item in the Toolbox and release the mouse button. Next, click the Employee object and then the Person object in the O/R Designer. The arrow on the inheritance line will point to the Person object.
Click the Inheritance line on the design surface.
Set the Discriminator Property property to Type.
Set the Derived Class Discriminator Value property to 2.
Set the Base Class Discriminator Value property to 1.
Set the Inheritance Default property to Person.
Build the project.
You will now add some code to the form that queries for a specific class in the object model.
To create a LINQ query and display the results on the form
Drag a ListBox onto Form1.
Double-click the form to create a Form1_Load event handler.
Add the following code to the Form1_Load event handler:
Dim dc As New DataClasses1DataContext Dim results = From emp In dc.Persons _ Where TypeOf emp Is Employee _ Select emp For Each Emp As Employee In results ListBox1.Items.Add(Emp.LastName) Next
Run the application and verify that the records displayed in the list box are all employees (records that have a value of 2 in their Type column).
To test the application
Verify that only records that have a value of 2 in their Type column are displayed.
Close the form. (On the Debug menu, click Stop Debugging.)