Walkthrough: Creating LINQ to SQL Classes by Using Single-Table Inheritance (O/R Designer)

 

For the latest documentation on Visual Studio 2017, see Walkthrough: Creating LINQ to SQL Classes by Using Single-Table Inheritance (O-R Designer) on docs.microsoft.com. The LINQ to SQL Tools in Visual Studio 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

  1. In Server Explorer/Database Explorer, right-click the Tables node and click Add New Table.

    System_CAPS_ICON_note.jpg Note

    You can use the Northwind database or any other database that you can add a table to.

  2. In the Table Designer, add the following columns to the table:

    Column NameData TypeAllow Nulls
    IDintFalse
    TypeintTrue
    FirstNamenvarchar(200)False
    LastNamenvarchar(200)False
    ManagerintTrue
  3. Set the ID column as the primary key.

  4. 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

  1. Open the table in data view. (Right-click the Person table in Server Explorer/Database Explorer and click Show Table Data.)

  2. 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.)

    IDTypeFirstNameLastNameManager
    11AnneWallaceNULL
    21CarlosGriloNULL
    31YaelPeledNULL
    42GatisOzolins1
    52AndreasHauser1
    62TiffanyPhuvasate1
    72AlexeyOrekhov2
    82MichałPoliszkiewicz2
    92TaiYee2
    102FabricioNoriega3
    112MindyMartin3
    122KenKwok3

Now that you have created the table, create a new project to demonstrate configuring inheritance.

To create the new Windows Application

  1. From the File menu, create a new project.

  2. Name the project InheritanceWalkthrough.

    System_CAPS_ICON_note.jpg Note

    The O/R Designer is supported in Visual Basic and C# projects. Create the new project in one of these languages.

  3. Click the Windows Forms Application template and then click OK. For more information, see Client Applications.

  4. The InheritanceWalkthrough project is created and added to Solution Explorer.

To add a LINQ to SQL File to the project

  1. On the Project menu, click Add New Item.

  2. Click the LINQ to SQL Classes template and then click Add.

    The .dbml file is added to the project and the O/R Designer opens.

Configure the inheritance by dragging an Inheritance object from the Toolbox onto the design surface.

To create the inheritance

  1. In Server Explorer/Database Explorer, navigate to the Person table that you created earlier.

  2. Drag the Person table onto the O/R Designer design surface.

  3. Drag a second Person table onto the O/R Designer and change its name to Employee.

  4. Delete the Manager property from the Person object.

  5. Delete the Type, ID, FirstName, and LastName properties from the Employee object. (In other words, delete all properties except for Manager.)

  6. 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.

  7. Click the Inheritance line on the design surface.

  8. Set the Discriminator Property property to Type.

  9. Set the Derived Class Discriminator Value property to 2.

  10. Set the Base Class Discriminator Value property to 1.

  11. Set the Inheritance Default property to Person.

  12. 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

  1. Drag a ListBox onto Form1.

  2. Double-click the form to create a Form1_Load event handler.

  3. Add the following code to the Form1_Load event handler:

    NorthwindDataContext dc = new DataClasses1DataContext();  
    var results = from emp in dc.Persons  
                  where emp is Employee  
                  select emp;  
    
    foreach(Employee Emp in results)  
    {  
        listBox1.Items.Add(Emp.LastName)  
    }  
    
    

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

  1. Press F5.

  2. Verify that only records that have a value of 2 in their Type column are displayed.

  3. Close the form. (On the Debug menu, click Stop Debugging.)

LINQ to SQL Tools in Visual Studio
How to: Add LINQ to SQL Classes to a Project (O-R Designer)
Walkthrough: Creating LINQ to SQL Classes (O-R Designer)
How to: Assign stored procedures to perform updates, inserts, and deletes (O/R Designer)
LINQ to SQL
How to: Generate the Object Model in Visual Basic or C#

Show: