共用方式為


HOW TO:加入及修改具有每個類型的資料表繼承的物件 (Entity Framework)

本主題使用 HOW TO:使用每個類型的資料表繼承來定義模型 (Entity Framework) 主題中所定義的 Entity Data Model (EDM)。它示範如何將衍生型別的執行個體加入到每個類型的資料表繼承階層架構中。

若要使用每個類型的資料表繼承來建立專案

  1. 建立名為 SchoolDataClient 的主控台應用程式專案,並加入 System.Data.EntitySystem.Runtime.Serialization 的參考。

  2. 加入從 HOW TO:使用每個類型的資料表繼承來定義模型 (Entity Framework) 所述之繼承階層架構建置而來之 dll 的參考。

  3. 將主題 HOW TO:使用每個類型的資料表繼承來定義模型 (Entity Framework) 中的結構描述加入至與 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. 建立名為 newDeptDeptEngineering 型別的執行個體。

  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());
            }
        }
    }
}

另請參閱

工作

HOW TO:使用每個類型的資料表繼承來定義模型 (Entity Framework)
HOW TO:使用每個類型的資料表繼承來建立和執行物件查詢 (Entity Framework)