本示例使用主题如何:通过每个层次结构一个表继承以定义模型(实体框架) 中设计的实体数据模型 (EDM)。
使用每个层次结构一个表继承模型创建项目
创建一个控制台应用程序项目,并添加对 System.Data.Entity 和 System.Runtime.Serialization 的引用。
添加一个对从主题如何:通过每个层次结构一个表继承以定义模型(实体框架) 中的继承模型生成的 dll 的引用。
将主题如何:通过每个层次结构一个表继承以定义模型(实体框架) 中的架构添加到 SchoolDataClient 可执行文件所在的文件夹。
添加一个应用程序配置文件,其内容如下面的示例所示。
<?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>
添加新的 Instructor 和 Student 派生类型并修改 Instructor
实例化 SchoolDataEntities 对象上下文。
创建 Instructor 类型的新实例并且向 Instructor 属性分配数据。
使用 AddToPeople 方法向存储中添加 Instructor 的新实例。AddToPeople 方法的参数为新 Instructor 实例的名称。
保存更改。
创建 Student 类型的新实例并初始化属性。
添加到存储并保存更改。
创建一个要在针对属性 LastName 等于 Griffin Instructor 的实例的查询中使用的 ObjectParameter。
测试该查询以验证具有此属性的 Instructor 存在于存储中。
查询指定的 Instructor 并将其赋给一个名为 changeInstructor 的变量。
将属性 LastName 分配给 Anderson,并保存更改。
示例
以下代码添加 Instructor 类型和 Student 类型的新实例。接下来,它查询 LastName 为 Griffin 的 Instructor 并将其 LastName 属性更改为 Anderson。
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
Dim countDept As Integer = 0
Dim newDept As DeptEngineering = New DeptEngineering()
For Each dept As Department In objectContext.Departments
countDept = countDept + 1
Next
newDept.DepartmentID = countDept + 1
newDept.Name = "Engineering School " + (countDept + 1).ToString()
newDept.StartDate = DateTime.Now
newDept.FiberOpticsBudget = 250000.0
newDept.LabBudget = 400000.0
newDept.Budget = newDept.FiberOpticsBudget + newDept.LabBudget
' Create new contact item to be
' added as school administrator.
Dim countPerson As Integer = 0
For Each pers As Person In objectContext.People
countPerson = countPerson + 1
Next
Dim newAdmin As Administrator = New Administrator()
newAdmin.PersonID = countPerson + 1
newAdmin.FirstName = "Tony"
newAdmin.LastName = "Allen"
newAdmin.AdminDate = DateTime.Now - New TimeSpan(2000, 0, 0, 0)
' Assign the contact to Administrator property.
newDept.Administrator = newAdmin
' Add admin and school to object context.
objectContext.AddToPeople(newAdmin)
objectContext.AddToDepartments(newDept)
objectContext.SaveChanges()
End Using
Catch ex As System.Data.MappingException
Console.WriteLine(ex.ToString())
Catch ex As System.Data.CommandExecutionException
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 SchoolDataLibTPH;
using System.Data.Objects;
namespace SchoolDataLib
{
class Program
{
static void Main(string[] args)
{
try
{
// Add new Instructor.
using (SchoolDataLibContainer objectContext =
new SchoolDataLibContainer())
{
Instructor newInstructor =
new Instructor();
newInstructor.PersonID =
objectContext.People.Count<Person>() + 1;
newInstructor.FirstName = "Satomi";
newInstructor.LastName = "Hayakawa";
newInstructor.HireDate = DateTime.Now;
objectContext.AddToPeople(newInstructor);
objectContext.SaveChanges();
// Add new Student.
Student newStudent = new Student();
int count = objectContext.People.Count<Person>();
newStudent.PersonID = count + 1;
newStudent.FirstName = "Uzi";
newStudent.LastName = "Hefetz";
newStudent.EnrollmentDate = DateTime.Now;
objectContext.AddToPeople(newStudent);
objectContext.SaveChanges();
// Change the last name of an instructor.
ObjectParameter param =
new ObjectParameter("p", "Hayakawa");
if (0 != objectContext.People.OfType<Instructor>().
Where("it.LastName = @p",
param).Count<Instructor>())
{
Instructor changeInstructor =
objectContext.People.
OfType<Instructor>().Where(
"it.LastName = @p", param).First();
changeInstructor.LastName = "Anderson";
objectContext.SaveChanges();
}
objectContext.Connection.Close();
}
}
catch (System.Data.MappingException e)
{
Console.WriteLine(e.ToString());
}
catch (System.Data.UpdateException e)
{
Console.WriteLine(e.ToString());
}
}
}
}