如何:定义自定义对象上下文(实体框架)

在使用 POCO 实体时,可通过实体框架 禁用对象层生成。 除定义自定义实体类型外,还需要通过您传递到 ObjectContext 的构造函数的 EntityConnection 的手动创建实例,来定义自定义对象上下文类型或管理您自己的连接。 有关如何创建 EntityConnection 的信息,请参见如何:生成 EntityConnection 连接字符串(实体框架)

本主题演示如何创建自定义对象上下文。

Bb738471.note(zh-cn,VS.100).gif注意:
若要使用 ADO.NET Entity Data Model Designer(实体设计器)禁用对象层生成,请在实体设计器中打开 .edmx 文件。右击设计器图面并选择“属性”。在“属性”窗口中选择“代码生成策略”属性,并选择 None

自定义对象上下文类管理如何:定义 POCO 实体(实体框架)中定义的 POCO 实体。

自定义对象上下文应包含以下功能:

  • 实例化概念模型特定的 ObjectContext(包括预定义连接)的能力。

  • 返回特定于类型的 ObjectSet 对象的属性。

有关定义自定义实体类型的信息,请参见如何:定义 POCO 实体(实体框架)

使用自定义对象上下文代码中的生成的对象上下文代码

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

示例

本示例演示支持 ContactOrder、和 LineItem 自定义数据类的自定义对象上下文代码。

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)

概念

自定义对象(实体框架)

其他资源

How to: Customize Object-Layer Code Generation