Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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.

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

Community Additions

Show:
© 2015 Microsoft