如何:使用每个层次结构一个表继承创建和执行对象查询(实体框架)
实体数据模型 (EDM) 中的继承提供了应用于面向对象编程的继承的一些优势。层次结构中类型的实例可以在其中一个子类型的实例上一起运行,或者作为其中一个子类型的实例运行。所有子类型都继承基类型的属性,并且可以根据需要访问由子类型实现的其他属性。
使用每个层次结构一个表继承创建项目
创建一个控制台应用程序项目,并添加对 System.Data.Entity 和 System.Runtime.Serialization 的引用。
添加一个对从主题如何:通过每个层次结构一个表继承以定义模型(实体框架) 中的继承模型生成的 dll 的引用。
将主题如何:通过每个层次结构一个表继承以定义模型(实体框架) 中的架构添加到与可执行文件所在的同一个文件夹。
添加一个应用程序配置文件,其中包含以下示例中的内容。
<?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="
Data Source=localhost;
Initial Catalog=SchoolData;Integrated Security=True;
MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
示例
以下代码通过 foreach
循环显示层次结构中各类型的所有实例,此循环枚举 SchoolDataEntitiesObjectContext 的 People 集合中各类型的实例。
将通过调用 GetType 方法显示每个实例的类型。
第二个 foreach
循环对于 People 集合使用 OfType 筛选器,以只返回类型为 Instructor 的实例。
只有 Instructor(教师)类型才包含 HireDate 属性。此代码显示由查询返回的教师的 HireDate 以及姓氏和名字。
Option Strict On
Option Explicit On
Imports System
Imports System.Collections.Generic
Imports System.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 person reference.
dept.AdministratorReference.Load()
If dept.Administrator IsNot Nothing Then
Console.WriteLine("Administrator: {0} {1}", _
dept.Administrator.FirstName, _
dept.Administrator.LastName)
End If
For Each student As Student In objectContext.People.OfType(Of Student)()
Console.WriteLine("Student: {0} {1}", student.FirstName, _
student.LastName)
Next
For Each person As Person In objectContext.People
Console.WriteLine("{0} Name: {1}", _
person.GetType().Name, person.LastName)
Next
For Each instructor In objectContext.People. _
OfType(Of Instructor)()
Console.WriteLine("Instructor: {0} {1} Hire Date: {2}", _
instructor.FirstName, _
instructor.LastName, _
instructor.HireDate)
Next
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;
using System.Data.Objects;
namespace SchoolDataClient
{
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 person reference.
dept.AdministratorReference.Load();
if (null != dept.Administrator)
Console.WriteLine("Administrator: {0} {1}",
dept.Administrator.FirstName,
dept.Administrator.LastName);
}
foreach (Student student in
objectContext.People.OfType<Student>())
{
Console.WriteLine("Student: {0} {1}", student.FirstName,
student.LastName);
}
foreach (Person person in objectContext.People)
Console.WriteLine("{0} Name: {1}",
person.GetType().Name,
person.LastName);
foreach (Instructor instructor in
objectContext.People.OfType<Instructor>())
Console.WriteLine("Instructor: {0} {1} Hire Date: {2}",
instructor.FirstName,
instructor.LastName,
instructor.HireDate);
objectContext.Connection.Close();
}
}
catch (System.Data.MappingException e)
{
Console.WriteLine(e.ToString());
}
catch (System.Data.UpdateException e)
{
Console.WriteLine(e.ToString());
}
}
}
}