ObjectContext ObjectContext ObjectContext ObjectContext Class

定義

提供以物件形式查詢和使用實體資料的機能。Provides facilities for querying and working with entity data as objects.

public ref class ObjectContext : IDisposable
public class ObjectContext : IDisposable
type ObjectContext = class
    interface IDisposable
Public Class ObjectContext
Implements IDisposable
繼承
ObjectContextObjectContextObjectContextObjectContext
實作

範例

這個範例根據AdventureWorks Sales ModelThis example is based on the AdventureWorks Sales Model. 此範例將示範如何建構 ObjectContextThe example shows you how to construct an ObjectContext.

// Create the ObjectContext.
ObjectContext context =
    new ObjectContext("name=AdventureWorksEntities");

// Set the DefaultContainerName for the ObjectContext.
// When DefaultContainerName is set, the Entity Framework only
// searches for the type in the specified container. 
// Note that if a type is defined only once in the metadata workspace
// you do not have to set the DefaultContainerName.
context.DefaultContainerName = "AdventureWorksEntities";

ObjectSet<Product> query = context.CreateObjectSet<Product>();

// Iterate through the collection of Products.
foreach (Product result in query)
    Console.WriteLine("Product Name: {0}", result.Name);
' Create the ObjectContext. 
Dim context As New ObjectContext("name=AdventureWorksEntities")

' Set the DefaultContainerName for the ObjectContext.
' When DefaultContainerName is set, the Entity Framework only
' searches for the type in the specified container. 
' Note that if a type is defined only once in the metadata workspace
' you do not have to set the DefaultContainerName.
context.DefaultContainerName = "AdventureWorksEntities"

Dim query As ObjectSet(Of Product) = context.CreateObjectSet(Of Product)()

' Iterate through the collection of Products. 
For Each result As Product In query
    Console.WriteLine("Product Name: {0}", result.Name)
Next

備註

注意

ObjectContext 類別不具備執行緒安全。The ObjectContext class is not thread safe. 中的資料物件的完整性ObjectContext無法確保多執行緒案例中。The integrity of data objects in an ObjectContext cannot be ensured in multithreaded scenarios.

ObjectContext類別是做為概念模型中定義的實體類型的執行個體的物件與資料互動的主要類別。The ObjectContext class is the primary class for interacting with data as objects that are instances of entity types that are defined in a conceptual model. ObjectContext 類別的執行個體會封裝下列項目:An instance of the ObjectContext class encapsulates the following:

當 Entity Data Model 工具產生代表概念模型的物件層時,此類別,代表EntityContainer模型衍生自ObjectContextWhen the object layer that represents a conceptual model is generated by the Entity Data Model tools, the class that represents the EntityContainer for the model is derived from the ObjectContext.

建構函式

ObjectContext(EntityConnection) ObjectContext(EntityConnection) ObjectContext(EntityConnection) ObjectContext(EntityConnection)

使用給定的連接,初始化 ObjectContext 類別的新執行個體。Initializes a new instance of the ObjectContext class with the given connection. 在建構期間,系統會從 EntityConnection 物件擷取中繼資料工作區。During construction, the metadata workspace is extracted from the EntityConnection object.

ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String)

使用給定的連接和實體容器名稱,初始化 ObjectContext 類別的新執行個體。Initializes a new instance of the ObjectContext class with a given connection and entity container name.

ObjectContext(String) ObjectContext(String) ObjectContext(String) ObjectContext(String)

使用給定的連接字串和預設實體容器名稱,初始化 ObjectContext 類別的新執行個體。Initializes a new instance of the ObjectContext class with the given connection string and default entity container name.

ObjectContext(String, String) ObjectContext(String, String) ObjectContext(String, String) ObjectContext(String, String)

使用給定的連接字串和實體容器名稱,初始化 ObjectContext 類別的新執行個體。Initializes a new instance of the ObjectContext class with a given connection string and entity container name.

屬性

CommandTimeout CommandTimeout CommandTimeout CommandTimeout

取得或設定所有物件內容作業的逾時值 (以秒為單位)。Gets or sets the timeout value, in seconds, for all object context operations. null 值表示將會使用基礎提供者 (Provider) 的預設值。A null value indicates that the default value of the underlying provider will be used.

Connection Connection Connection Connection

取得物件內容所使用的連接。Gets the connection used by the object context.

ContextOptions ContextOptions ContextOptions ContextOptions

取得 ObjectContextOptions 執行個體,這個執行個體包含影響 ObjectContext 之行為的選項。Gets the ObjectContextOptions instance that contains options that affect the behavior of the ObjectContext.

DefaultContainerName DefaultContainerName DefaultContainerName DefaultContainerName

取得或設定預設容器名稱。Gets or sets the default container name.

MetadataWorkspace MetadataWorkspace MetadataWorkspace MetadataWorkspace

取得物件內容所使用的中繼資料工作區。Gets the metadata workspace used by the object context.

ObjectStateManager ObjectStateManager ObjectStateManager ObjectStateManager

取得物件內容用來追蹤物件變更的物件狀態管理員。Gets the object state manager used by the object context to track object changes.

QueryProvider QueryProvider QueryProvider QueryProvider

取得與這個物件內容相關聯的 LINQ 查詢提供者。Gets the LINQ query provider associated with this object context.

方法

AcceptAllChanges() AcceptAllChanges() AcceptAllChanges() AcceptAllChanges()

接受對物件內容中之物件所做的所有變更。Accepts all changes made to objects in the object context.

AddObject(String, Object) AddObject(String, Object) AddObject(String, Object) AddObject(String, Object)

將物件加入至物件內容。Adds an object to the object context.

ApplyCurrentValues<TEntity>(String, TEntity) ApplyCurrentValues<TEntity>(String, TEntity) ApplyCurrentValues<TEntity>(String, TEntity) ApplyCurrentValues<TEntity>(String, TEntity)

將純量值從提供的物件複製到 ObjectContext 中具有相同索引鍵的物件。Copies the scalar values from the supplied object into the object in the ObjectContext that has the same key.

ApplyOriginalValues<TEntity>(String, TEntity) ApplyOriginalValues<TEntity>(String, TEntity) ApplyOriginalValues<TEntity>(String, TEntity) ApplyOriginalValues<TEntity>(String, TEntity)

將純量值從提供的物件複製到 ObjectContext 中具有相同索引鍵的物件的原始值集。Copies the scalar values from the supplied object into set of original values for the object in the ObjectContext that has the same key.

ApplyPropertyChanges(String, Object) ApplyPropertyChanges(String, Object) ApplyPropertyChanges(String, Object) ApplyPropertyChanges(String, Object)

將已中斷連結物件中的屬性變更套用至已經附加至物件內容的物件。Applies property changes from a detached object to an object already attached to the object context.

Attach(IEntityWithKey) Attach(IEntityWithKey) Attach(IEntityWithKey) Attach(IEntityWithKey)

當物件具有實體索引鍵時,將物件或物件圖形附加至物件內容。Attaches an object or object graph to the object context when the object has an entity key.

AttachTo(String, Object) AttachTo(String, Object) AttachTo(String, Object) AttachTo(String, Object)

將物件或物件圖形附加至位於特定實體集中的物件內容。Attaches an object or object graph to the object context in a specific entity set.

CreateDatabase() CreateDatabase() CreateDatabase() CreateDatabase()

使用目前資料來源連接和 StoreItemCollection 中的中繼資料,建立資料庫。Creates the database by using the current data source connection and the metadata in the StoreItemCollection.

CreateDatabaseScript() CreateDatabaseScript() CreateDatabaseScript() CreateDatabaseScript()

產生資料定義語言 (DDL) 指令碼,這個指令碼會針對 StoreItemCollection 中的中繼資料建立結構描述物件 (資料表、主索引鍵、外部索引鍵)。Generates a data definition language (DDL) script that creates schema objects (tables, primary keys, foreign keys) for the metadata in the StoreItemCollection. StoreItemCollection 會從存放區結構描述定義語言 (SSDL) 檔載入中繼資料。The StoreItemCollection loads metadata from store schema definition language (SSDL) files.

CreateEntityKey(String, Object) CreateEntityKey(String, Object) CreateEntityKey(String, Object) CreateEntityKey(String, Object)

建立特定物件的實體索引鍵,或傳回實體索引鍵 (如果已經存在的話)。Creates the entity key for a specific object, or returns the entity key if it already exists.

CreateObject<T>() CreateObject<T>() CreateObject<T>() CreateObject<T>()

建立和傳回所要求之類型的執行個體。Creates and returns an instance of the requested type .

CreateObjectSet<TEntity>() CreateObjectSet<TEntity>() CreateObjectSet<TEntity>() CreateObjectSet<TEntity>()

建立新的 ObjectSet<TEntity> 執行個體,這個執行個體用來查詢、加入、修改和刪除指定之實體型別的物件。Creates a new ObjectSet<TEntity> instance that is used to query, add, modify, and delete objects of the specified entity type.

CreateObjectSet<TEntity>(String) CreateObjectSet<TEntity>(String) CreateObjectSet<TEntity>(String) CreateObjectSet<TEntity>(String)

建立新的 ObjectSet<TEntity> 執行個體,這個執行個體用來查詢、加入、修改和刪除具有指定之型別和指定之實體集名稱的物件。Creates a new ObjectSet<TEntity> instance that is used to query, add, modify, and delete objects of the specified type and with the specified entity set name.

CreateProxyTypes(IEnumerable<Type>) CreateProxyTypes(IEnumerable<Type>) CreateProxyTypes(IEnumerable<Type>) CreateProxyTypes(IEnumerable<Type>)

針對所提供之列舉中的每一個型別,產生可以與 Entity Framework 一起使用的對等型別。Generates an equivalent type that can be used with the Entity Framework for each type in the supplied enumeration.

CreateQuery<T>(String, ObjectParameter[]) CreateQuery<T>(String, ObjectParameter[]) CreateQuery<T>(String, ObjectParameter[]) CreateQuery<T>(String, ObjectParameter[])

使用指定的查詢字串,在目前的物件內容中建立 ObjectQuery<T>Creates an ObjectQuery<T> in the current object context by using the specified query string.

DatabaseExists() DatabaseExists() DatabaseExists() DatabaseExists()

檢查在目前資料來源連接中指定為資料庫的資料庫是否存在於資料來源上。Checks if the database that is specified as the database in the current data source connection exists on the data source.

DeleteDatabase() DeleteDatabase() DeleteDatabase() DeleteDatabase()

刪除在目前資料來源連接中指定為資料庫的資料庫。Deletes the database that is specified as the database in the current data source connection.

DeleteObject(Object) DeleteObject(Object) DeleteObject(Object) DeleteObject(Object)

將物件標記為待刪除。Marks an object for deletion.

Detach(Object) Detach(Object) Detach(Object) Detach(Object)

從物件內容中移除物件。Removes the object from the object context.

DetectChanges() DetectChanges() DetectChanges() DetectChanges()

確保 ObjectStateEntry 變更與 ObjectStateManager 所追蹤之所有物件中的變更同步化。Ensures that ObjectStateEntry changes are synchronized with changes in all objects that are tracked by the ObjectStateManager.

Dispose() Dispose() Dispose() Dispose()

釋放物件內容所使用的資源。Releases the resources used by the object context.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放物件內容所使用的資源。Releases the resources used by the object context.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
ExecuteFunction(String, ObjectParameter[]) ExecuteFunction(String, ObjectParameter[]) ExecuteFunction(String, ObjectParameter[]) ExecuteFunction(String, ObjectParameter[])

執行資料來源中所定義和以概念模型表示的預存程序或函式、捨棄函式傳回的任何結果,然後傳回受執行所影響的資料列數目。Executes a stored procedure or function that is defined in the data source and expressed in the conceptual model; discards any results returned from the function; and returns the number of rows affected by the execution.

ExecuteFunction<TElement>(String, MergeOption, ObjectParameter[]) ExecuteFunction<TElement>(String, MergeOption, ObjectParameter[]) ExecuteFunction<TElement>(String, MergeOption, ObjectParameter[]) ExecuteFunction<TElement>(String, MergeOption, ObjectParameter[])

使用指定的參數和合併選項,執行資料來源中所定義和以概念模型表示之指定的預存程序或函式。Executes the given stored procedure or function that is defined in the data source and expressed in the conceptual model, with the specified parameters, and merge option. 傳回具型別的 ObjectResult<T>Returns a typed ObjectResult<T>.

ExecuteFunction<TElement>(String, ObjectParameter[]) ExecuteFunction<TElement>(String, ObjectParameter[]) ExecuteFunction<TElement>(String, ObjectParameter[]) ExecuteFunction<TElement>(String, ObjectParameter[])

使用指定的參數,執行資料來源中所定義且在概念模型中有所對應的預存程序或函式。Executes a stored procedure or function that is defined in the data source and mapped in the conceptual model, with the specified parameters. 傳回具型別的 ObjectResult<T>Returns a typed ObjectResult<T>.

ExecuteStoreCommand(String, Object[]) ExecuteStoreCommand(String, Object[]) ExecuteStoreCommand(String, Object[]) ExecuteStoreCommand(String, Object[])

使用現有的連接,直接針對資料來源執行任意命令。Executes an arbitrary command directly against the data source using the existing connection.

ExecuteStoreQuery<TElement>(String, Object[]) ExecuteStoreQuery<TElement>(String, Object[]) ExecuteStoreQuery<TElement>(String, Object[]) ExecuteStoreQuery<TElement>(String, Object[])

直接針對資料來源執行查詢,並傳回具型別的結果序列。Executes a query directly against the data source that returns a sequence of typed results.

ExecuteStoreQuery<TEntity>(String, String, MergeOption, Object[]) ExecuteStoreQuery<TEntity>(String, String, MergeOption, Object[]) ExecuteStoreQuery<TEntity>(String, String, MergeOption, Object[]) ExecuteStoreQuery<TEntity>(String, String, MergeOption, Object[])

直接針對資料來源執行查詢,並傳回具型別的結果序列。Executes a query directly against the data source and returns a sequence of typed results. 指定實體集和合併選項,以便能夠以實體形式追蹤查詢結果。Specify the entity set and the merge option so that query results can be tracked as entities.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetKnownProxyTypes() GetKnownProxyTypes() GetKnownProxyTypes() GetKnownProxyTypes()

傳回所有現有的 Proxy 型別。Returns all the existing proxy types.

GetObjectByKey(EntityKey) GetObjectByKey(EntityKey) GetObjectByKey(EntityKey) GetObjectByKey(EntityKey)

傳回具有指定之實體索引鍵的物件。Returns an object that has the specified entity key.

GetObjectType(Type) GetObjectType(Type) GetObjectType(Type) GetObjectType(Type)

傳回與所指定型別的 Proxy 物件相關聯的 POCO 實體的實體型別。Returns the entity type of the POCO entity associated with a proxy object of a specified type.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
LoadProperty(Object, String) LoadProperty(Object, String) LoadProperty(Object, String) LoadProperty(Object, String)

由指定的巡覽屬性和使用預設合併選項,明確地載入所提供之物件的相關物件。Explicitly loads an object related to the supplied object by the specified navigation property and using the default merge option.

LoadProperty(Object, String, MergeOption) LoadProperty(Object, String, MergeOption) LoadProperty(Object, String, MergeOption) LoadProperty(Object, String, MergeOption)

由指定的巡覽屬性和使用指定的合併選項,明確地載入所提供之物件的相關物件。Explicitly loads an object that is related to the supplied object by the specified navigation property and using the specified merge option.

LoadProperty<TEntity>(TEntity, Expression<Func<TEntity,Object>>) LoadProperty<TEntity>(TEntity, Expression<Func<TEntity,Object>>) LoadProperty<TEntity>(TEntity, Expression<Func<TEntity,Object>>) LoadProperty<TEntity>(TEntity, Expression<Func<TEntity,Object>>)

由指定的 LINQ 查詢和使用預設的合併選項,明確地載入所提供之物件的相關物件。Explicitly loads an object that is related to the supplied object by the specified LINQ query and by using the default merge option.

LoadProperty<TEntity>(TEntity, Expression<Func<TEntity,Object>>, MergeOption) LoadProperty<TEntity>(TEntity, Expression<Func<TEntity,Object>>, MergeOption) LoadProperty<TEntity>(TEntity, Expression<Func<TEntity,Object>>, MergeOption) LoadProperty<TEntity>(TEntity, Expression<Func<TEntity,Object>>, MergeOption)

由指定的 LINQ 查詢和使用指定的合併選項,明確地載入所提供之物件的相關物件。Explicitly loads an object that is related to the supplied object by the specified LINQ query and by using the specified merge option.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Refresh(RefreshMode, IEnumerable) Refresh(RefreshMode, IEnumerable) Refresh(RefreshMode, IEnumerable) Refresh(RefreshMode, IEnumerable)

使用資料來源中的資料,更新物件內容中的物件集合。Updates a collection of objects in the object context with data from the data source.

Refresh(RefreshMode, Object) Refresh(RefreshMode, Object) Refresh(RefreshMode, Object) Refresh(RefreshMode, Object)

使用資料來源中的資料,更新物件內容中的物件。Updates an object in the object context with data from the data source.

SaveChanges() SaveChanges() SaveChanges() SaveChanges()

將所有更新保存至資料來源並且重設物件內容中的變更追蹤。Persists all updates to the data source and resets change tracking in the object context.

SaveChanges(Boolean) SaveChanges(Boolean) SaveChanges(Boolean) SaveChanges(Boolean)

將所有更新保存至資料來源,並且選擇性地重設物件內容中的變更追蹤。Persists all updates to the data source and optionally resets change tracking in the object context.

SaveChanges(SaveOptions) SaveChanges(SaveOptions) SaveChanges(SaveOptions) SaveChanges(SaveOptions)

使用指定的 SaveOptions,將所有更新保存至資料來源。Persists all updates to the data source with the specified SaveOptions.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
Translate<TElement>(DbDataReader) Translate<TElement>(DbDataReader) Translate<TElement>(DbDataReader) Translate<TElement>(DbDataReader)

將包含實體資料之資料列的 DbDataReader,轉譯成所要求的實體型別。Translates a DbDataReader that contains rows of entity data to objects of the requested entity type.

Translate<TEntity>(DbDataReader, String, MergeOption) Translate<TEntity>(DbDataReader, String, MergeOption) Translate<TEntity>(DbDataReader, String, MergeOption) Translate<TEntity>(DbDataReader, String, MergeOption)

在特定的實體集並使用指定的合併選項,將包含實體資料之資料列的 DbDataReader 轉譯為所要求之實體型別的物件。Translates a DbDataReader that contains rows of entity data to objects of the requested entity type, in a specific entity set, and with the specified merge option.

TryGetObjectByKey(EntityKey, Object) TryGetObjectByKey(EntityKey, Object) TryGetObjectByKey(EntityKey, Object) TryGetObjectByKey(EntityKey, Object)

傳回具有指定之實體索引鍵的物件。Returns an object that has the specified entity key.

事件

ObjectMaterialized ObjectMaterialized ObjectMaterialized ObjectMaterialized

在查詢或載入作業中從資料來源中的資料建立新的實體物件時發生。Occurs when a new entity object is created from data in the data source as part of a query or load operation.

SavingChanges SavingChanges SavingChanges SavingChanges

當變更儲存至資料來源時發生。Occurs when changes are saved to the data source.

適用於

另請參閱