ObjectQuery.Context 属性

定义

获取与此对象查询相关联的对象上下文。Gets the object context associated with this object query.

public:
 property System::Data::Objects::ObjectContext ^ Context { System::Data::Objects::ObjectContext ^ get(); };
public System.Data.Objects.ObjectContext Context { get; }
member this.Context : System.Data.Objects.ObjectContext
Public ReadOnly Property Context As ObjectContext

属性值

与此 ObjectContext 实例关联的 ObjectQuery<T>The ObjectContext associated with this ObjectQuery<T> instance.

示例

本主题中的示例基于AdventureWorks 销售模型The example in this topic is based on the AdventureWorks Sales Model. 此示例获取与此 ObjectContext 对象关联的 ObjectQuery<T> 对象。The example gets the ObjectContext object associated with this ObjectQuery<T> object.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string queryString =
        @"SELECT VALUE contact FROM 
        AdventureWorksEntities.Contacts AS contact";

    ObjectQuery<Contact> contactQuery =
        new ObjectQuery<Contact>(queryString,
            context, MergeOption.NoTracking);

    // Get ObjectContext from ObjectQuery.
    ObjectContext objectContext = contactQuery.Context;
    Console.WriteLine("Connection string {0}",
        objectContext.Connection.ConnectionString);
}
Using context As New AdventureWorksEntities()
    Dim queryString As String = "SELECT VALUE contact FROM AdventureWorksEntities.Contacts AS contact"

    Dim contactQuery As New ObjectQuery(Of Contact)(queryString, context, MergeOption.NoTracking)

    ' Get ObjectContext from ObjectQuery. 
    Dim objectContext As ObjectContext = contactQuery.Context
    Console.WriteLine("Connection string {0}", objectContext.Connection.ConnectionString)
End Using

注解

查询的上下文包括连接、缓存和元数据。The context for the query includes the connection, cache, and metadata. 有关详细信息,请参阅对象服务概述 (Entity Framework)For more information, see Object Services Overview (Entity Framework).

连接属性是可变的,执行查询前必须设置该属性。The connection property is mutable and must be set before a query can be executed. 有关详细信息, 请参阅管理对象服务中的连接 (实体框架)For more information, see Managing Connections in Object Services (Entity Framework).

适用于

另请参阅