共用方式為


HOW TO:定義自訂物件內容 (Entity Framework)

當您使用 POCO 實體,會停用 Entity Framework 的物件層產生。 除了定義自訂實體類型,您必須透過手動建立的 EntityConnection 的執行個體 (您傳遞給 ObjectContext 之建構函式的執行個體),定義自訂物件內容類型或管理您自己的連接。 如需如何建立 EntityConnection 的詳細資訊,請參閱 HOW TO:建立 EntityConnection 連接字串 (Entity Framework)

本主題也將示範如何建立自訂物件內容。

Bb738471.note(zh-tw,VS.100).gif注意:
若要使用 ADO.NET Entity Data Model Designer (Entity Designer) 來停用物件層的產生,請在 Entity Designer 中開啟 .edmx 檔案。以滑鼠右鍵按一下設計工具介面並選取 [屬性]。在 [屬性] 視窗中,選取 [程式碼產生策略] 屬性並選取 [None]。

自訂物件內容類別會管理 HOW TO:定義 POCO 實體 (Entity Framework)中所定義的 POCO 實體。

自訂物件內容應該包含下列功能:

  • 具現化您的概念模型特有之 ObjectContext (包括預先定義的連接) 的功能。

  • 可傳回型別特有 ObjectSet 物件的屬性。

如需定義自訂實體類型的詳細資訊,請參閱 HOW TO:定義 POCO 實體 (Entity Framework)

在自訂物件內容程式碼中使用產生的物件內容程式碼

  1. 將類別程式碼檔加入至您的專案。

  2. 包含下列命名空間:

    System

    System.Data.Objects

    System.Collections.Generic

  3. 將類別重新命名為 POCOAdventureWorksEntities。 請確定類別繼承自 ObjectContext 類別。

    Partial Public Class POCOAdventureWorksEntities
        Inherits ObjectContext
    
    public partial class POCOAdventureWorksEntities : ObjectContext
    
  4. 為每個 POCO 實體類型定義 ObjectSet 類型的成員變數:

    Private _contacts As ObjectSet(Of Contact)
    Private _lineItems As ObjectSet(Of LineItem)
    Private _orders As ObjectSet(Of Order)
    
    private ObjectSet<Contact> _contacts;
    private ObjectSet<LineItem> _lineItems;
    private ObjectSet<Order> _orders;
    
  5. 定義 POCOAdventureWorksEntities 類別的建構函式。

    Public Sub New()
        MyBase.New("name=AdventureWorksEntities", "AdventureWorksEntities")
        Me.ContextOptions.LazyLoadingEnabled = True
    End Sub
    
    Public Sub New(ByVal connectionString As String)
        MyBase.New(connectionString, "AdventureWorksEntities")
        Me.ContextOptions.LazyLoadingEnabled = True
    End Sub
    
     public POCOAdventureWorksEntities()
         : base("name=AdventureWorksEntities", "AdventureWorksEntities")
     {
         this.ContextOptions.LazyLoadingEnabled = true;
    }
    
     public POCOAdventureWorksEntities(string connectionString)
         : base(connectionString, "AdventureWorksEntities")
     {
         this.ContextOptions.LazyLoadingEnabled = true;
     }
    
  6. 定義會傳回 ObjectSet 物件的屬性。

    Public ReadOnly Property Contacts() As ObjectSet(Of Contact)
        Get
            Return If(_contacts, MyBase.CreateObjectSet(Of Contact)())
        End Get
    End Property
    
    Public ReadOnly Property LineItems() As ObjectSet(Of LineItem)
        Get
            Return If(_lineItems, MyBase.CreateObjectSet(Of LineItem)())
        End Get
    End Property
    
    Public ReadOnly Property Orders() As ObjectSet(Of Order)
        Get
            Return If(_orders, MyBase.CreateObjectSet(Of Order)())
        End Get
    End Property
    
    public ObjectSet<Contact> Contacts
    {
        get
        {
            return _contacts ?? (_contacts = base.CreateObjectSet<Contact>());
        }
    }
    
    public ObjectSet<LineItem> LineItems
    {
        get
        {
            return _lineItems ?? (_lineItems = base.CreateObjectSet<LineItem>());
        }
    }
    
    public ObjectSet<Order> Orders
    {
        get
        {
            return _orders ?? (_orders = base.CreateObjectSet<Order>());
        }
    }
    

範例

這個範例會說明支援 ContactOrderLineItem 自訂資料類別的自訂物件內容程式碼。

Partial Public Class POCOAdventureWorksEntities
    Inherits ObjectContext
    Private _contacts As ObjectSet(Of Contact)
    Private _lineItems As ObjectSet(Of LineItem)
    Private _orders As ObjectSet(Of Order)
#Region "Constructors"
    Public Sub New()
        MyBase.New("name=AdventureWorksEntities", "AdventureWorksEntities")
        Me.ContextOptions.LazyLoadingEnabled = True
    End Sub

    Public Sub New(ByVal connectionString As String)
        MyBase.New(connectionString, "AdventureWorksEntities")
        Me.ContextOptions.LazyLoadingEnabled = True
    End Sub
#End Region

#Region "ObjectSet Properties"
    Public ReadOnly Property Contacts() As ObjectSet(Of Contact)
        Get
            Return If(_contacts, MyBase.CreateObjectSet(Of Contact)())
        End Get
    End Property

    Public ReadOnly Property LineItems() As ObjectSet(Of LineItem)
        Get
            Return If(_lineItems, MyBase.CreateObjectSet(Of LineItem)())
        End Get
    End Property

    Public ReadOnly Property Orders() As ObjectSet(Of Order)
        Get
            Return If(_orders, MyBase.CreateObjectSet(Of Order)())
        End Get
    End Property

#End Region
    Public Overloads Overrides Function SaveChanges(ByVal options As SaveOptions) As Integer

        For Each entry As ObjectStateEntry In ObjectStateManager.GetObjectStateEntries(EntityState.Added Or EntityState.Modified)
            ' Validate the objects in the Added and Modified state 
            ' if the validation fails throw an exeption. 
        Next
        Return MyBase.SaveChanges(options)
    End Function

End Class
public partial class POCOAdventureWorksEntities : ObjectContext
{
    private ObjectSet<Contact> _contacts;
    private ObjectSet<LineItem> _lineItems;
    private ObjectSet<Order> _orders;
    #region Constructors
    public POCOAdventureWorksEntities()
        : base("name=AdventureWorksEntities", "AdventureWorksEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
   }

    public POCOAdventureWorksEntities(string connectionString)
        : base(connectionString, "AdventureWorksEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
    }
    #endregion

    #region ObjectSet Properties
    public ObjectSet<Contact> Contacts
    {
        get
        {
            return _contacts ?? (_contacts = base.CreateObjectSet<Contact>());
        }
    }

    public ObjectSet<LineItem> LineItems
    {
        get
        {
            return _lineItems ?? (_lineItems = base.CreateObjectSet<LineItem>());
        }
    }

    public ObjectSet<Order> Orders
    {
        get
        {
            return _orders ?? (_orders = base.CreateObjectSet<Order>());
        }
    }

    #endregion
    public override int SaveChanges(SaveOptions options)
    {

        foreach (ObjectStateEntry entry in
            ObjectStateManager.GetObjectStateEntries(
            EntityState.Added | EntityState.Modified))
        {
            // Validate the objects in the Added and Modified state
            // if the validation fails throw an exeption.
        }
        return base.SaveChanges(options);
    }

}

另請參閱

參考

EDM 產生器 (EdmGen.exe)

概念

自訂物件 (Entity Framework)

其他資源

How to: Customize Object-Layer Code Generation