如何:通过每种类型一个表继承添加和修改对象(实体框架)

本主题使用在主题如何:通过每种类型一个表继承以定义模型(实体框架) 中定义的实体数据模型 (EDM)。本主题演示如何在每种类型一个表继承层次结构中添加派生类型的实例。

创建使用每种类型一个表继承的项目

  1. 创建一个名为 SchoolDataClient 的控制台应用程序项目并添加对 System.Data.EntitySystem.Runtime.Serialization 的引用。

  2. 对于从在如何:通过每种类型一个表继承以定义模型(实体框架) 中介绍的继承层次结构中生成的 dll,添加针对它的引用。

  3. 将主题如何:通过每种类型一个表继承以定义模型(实体框架) 中的架构添加到 SchoolDataClient 可执行文件所在的文件夹。

  4. 向项目中添加一个应用程序配置文件,其内容如下。

<?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>

添加派生类型的实例

  1. 创建 DeptEngineering 类型的名为 newDept 的实例。

  2. 初始化 newDept 的属性的数据。

  3. 创建 Person 类型的一个新实例。

  4. PersonCategory 属性指定为 3 以表示 Administrator 类别。

  5. 初始化 newAdmin 的属性。

  6. 将新 Person 分配给 newDept 的 Administrator 属性。

  7. newDeptnewAdmin 添加到存储中并保存更改。

示例

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("People", newAdmin)
                objectContext.AddToDepartments("Departments", 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolDataLib;

namespace SchoolDataClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SchoolDataLibContainer objectContext =
                                      new SchoolDataLibContainer())
                {
                    DeptEngineering newDept =
                                         new DeptEngineering();

                    int countDept = 
                        objectContext.Departments.Count<Department>();
                    newDept.DepartmentID = countDept + 1;
                    newDept.Name = "Engineering School " +
                                          (countDept + 1).ToString();
                    newDept.StartDate = DateTime.Now;
                    newDept.FiberOpticsBudget = 250000.00M;
                    newDept.LabBudget = 400000.00M;
                    newDept.Budget = newDept.FiberOpticsBudget + 
                        newDept.LabBudget;

                    // Create new contact item to be 
                    // added as school administrator.
                    int countPerson = objectContext.People.Count<Person>();
                    Administrator newAdmin = new Administrator();
                    newAdmin.PersonID = countPerson + 1;
                    newAdmin.FirstName = "Jesper";
                    newAdmin.LastName = "Aaberg";
                    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();
                }
            }

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

另请参见

任务

如何:通过每种类型一个表继承以定义模型(实体框架)
如何:使用每种类型一个表继承创建和执行对象查询(实体框架)