How to: Create and Execute Object Queries using Table-per-Type Inheritance (Entity Framework)

Inheritance in the Entity Data Model (EDM) provides the same advantages of inheritance that apply in object-oriented programming. Instances of types in the hierarchy can be operated on together or as instances of one of the subtypes. All subtypes inherit the properties of the base type and additional properties implemented by the subtypes can be accessed as needed.

To create the project using table-per-type inheritance

  1. Create a console application project named SchoolDataClient and add references to System.Data.Entity and System.Runtime.Serialization.

  2. Add a reference to the dll built from the inheritance hierarchy described in How to: Define a Model with Table-per-Type Inheritance (Entity Framework).

  3. Add the schemas from the inheritance project to the same folder as the SchoolDataClient executable.

  4. Add an application configuration file to the project with the following content.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings><add name="SchoolDataLibContainer" 
   connectionString=
   "metadata=res://*/SchoolDataLib.csdl|
   res://*/SchoolDataLib.ssdl|
   res://*/SchoolDataLib.msl;provider=System.Data.SqlClient;
   provider connection string=&quot;
   Data Source=localhost;
   Initial Catalog=SchoolData;Integrated Security=True;
   MultipleActiveResultSets=True&quot;" 
   providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

Example

The following code displays data for all the departments in the system.

A foreach loop on the base type Department will display data for the DeptBusiness, DeptEngineering, and DeptMusic types derived from the Department type.

The second foreach loop shows how to get only the engineering schools from the Departments collection.

The OfType operator allows filtering of values from a source based on type.

The Departments collection contains departments of engineering, business, and music, but the generic specification returns only those of the type specified in the angle brackets.

Option Strict On
Option Explicit On
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml
Imports System.Xml.Linq
Imports System.Text
Imports SchoolDataLib

Module Module1

    Sub Main()
        Try
            Using objectContext As SchoolDataLibContainer = New SchoolDataLibContainer()
                ' Display departments and administrators.
                For Each dept As Department In objectContext.Departments
                    Console.WriteLine("School: {0} Budget: {1}", _
                            dept.Name, dept.Budget)

                    ' Load associated contact reference if any.
                    dept.AdministratorReference.Load()
                    If dept.Administrator IsNot Nothing Then
                        Console.WriteLine("Administrator: {0} {1}", _
                            dept.Administrator.FirstName, _
                            dept.Administrator.LastName)
                    End If
                Next

                For Each eDept As DeptEngineering In _
                        objectContext.Departments.OfType(Of DeptEngineering)()
                    Console.WriteLine("{0} LabBudget: {1} FiberOp Budget: {2}", _
                        eDept.Name, eDept.LabBudget.ToString(), _
                        eDept.FiberOpticsBudget.ToString())
                Next
            End Using
        Catch ex As System.Data.MappingException
            Console.WriteLine(ex.ToString())
        Catch ex As System.Data.UpdateException
            Console.WriteLine(ex.ToString())
        End Try
    End Sub

End Module
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolDataLib;

namespace SchoolDataTPTClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SchoolDataLibContainer objectContext =
                                      new SchoolDataLibContainer())
                {
                    // Display departments and administrators.
                    foreach (Department dept in objectContext.Departments)
                    {
                        Console.WriteLine("School: {0} Budget: {1}",
                            dept.Name,
                            dept.Budget);

                        // Load associated contact reference if any.
                        dept.AdministratorReference.Load();
                        if (null != dept.Administrator)
                            Console.WriteLine("Administrator: {0} {1}",
                                         dept.Administrator.FirstName,
                                         dept.Administrator.LastName);
                    }

                    foreach (DeptEngineering eDept in
                        objectContext.Departments.OfType<DeptEngineering>())
                        Console.WriteLine("{0} LabBudget: {1} FiberOp Budget: {2}",
                            eDept.Name, eDept.LabBudget.ToString(), 
                            eDept.FiberOpticsBudget.ToString());
                    }

                }

            catch (System.Data.MappingException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (System.Data.UpdateException e)
            {
                Console.WriteLine(e.ToString());
            }

        }
    }
}

See Also

Tasks

How to: Define a Model with Table-per-Type Inheritance (Entity Framework)