ObjectContext Class

Provides facilities for querying and working with entity data as objects.

System.Object
  System.Data.Objects.ObjectContext

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

'Declaration
Public Class ObjectContext _
	Implements IDisposable

The ObjectContext type exposes the following members.

  NameDescription
Public methodObjectContext(EntityConnection)Initializes a new instance of the ObjectContext class with the given connection. During construction, the metadata workspace is extracted from the EntityConnection object.
Public methodObjectContext(String)Initializes a new instance of the ObjectContext class with the given connection string and default entity container name.
Protected methodObjectContext(EntityConnection, String)Initializes a new instance of the ObjectContext class with a given connection and entity container name.
Protected methodObjectContext(String, String)Initializes a new instance of the ObjectContext class with a given connection string and entity container name.
Top

  NameDescription
Public propertyCommandTimeoutGets or sets the timeout value, in seconds, for all object context operations. A Nothing value indicates that the default value of the underlying provider will be used.
Public propertyConnectionGets the connection used by the object context.
Public propertyContextOptionsGets the ObjectContextOptions instance that contains options that affect the behavior of the ObjectContext.
Public propertyDefaultContainerNameGets or sets the default container name.
Public propertyMetadataWorkspaceGets the metadata workspace used by the object context.
Public propertyObjectStateManagerGets the object state manager used by the object context to track object changes.
Protected propertyQueryProviderGets the LINQ query provider associated with this object context.
Top

  NameDescription
Public methodAcceptAllChangesAccepts all changes made to objects in the object context.
Public methodAddObjectAdds an object to the object context.
Public methodApplyCurrentValues(Of TEntity)Copies the scalar values from the supplied object into the object in the ObjectContext that has the same key.
Public methodApplyOriginalValues(Of TEntity)Copies the scalar values from the supplied object into set of original values for the object in the ObjectContext that has the same key.
Public methodApplyPropertyChanges Obsolete. Applies property changes from a detached object to an object already attached to the object context.
Public methodAttachAttaches an object or object graph to the object context when the object has an entity key.
Public methodAttachToAttaches an object or object graph to the object context in a specific entity set.
Public methodCreateDatabaseCreates the database by using the current data source connection and the metadata in the StoreItemCollection.
Public methodCreateDatabaseScriptGenerates a data definition language (DDL) script that creates schema objects (tables, primary keys, foreign keys) for the metadata in the StoreItemCollection. The StoreItemCollection loads metadata from store schema definition language (SSDL) files.
Public methodCreateEntityKeyCreates the entity key for a specific object, or returns the entity key if it already exists.
Public methodCreateObject(Of T)Creates and returns an instance of the requested type .
Public methodCreateObjectSet(Of TEntity)Creates a new ObjectSet(Of TEntity) instance that is used to query, add, modify, and delete objects of the specified entity type.
Public methodCreateObjectSet(Of TEntity)(String)Creates a new ObjectSet(Of TEntity) instance that is used to query, add, modify, and delete objects of the specified type and with the specified entity set name.
Public methodCreateProxyTypesGenerates an equivalent type that can be used with the Entity Framework for each type in the supplied enumeration.
Public methodCreateQuery(Of T)Creates an ObjectQuery(Of T) in the current object context by using the specified query string.
Public methodDatabaseExistsChecks if the database that is specified as the database in the current data source connection exists on the data source.
Public methodDeleteDatabaseDeletes the database that is specified as the database in the current data source connection.
Public methodDeleteObjectMarks an object for deletion.
Public methodDetachRemoves the object from the object context.
Public methodDetectChangesEnsures that ObjectStateEntry changes are synchronized with changes in all objects that are tracked by the ObjectStateManager.
Public methodDisposeReleases the resources used by the object context.
Protected methodDispose(Boolean)Releases the resources used by the object context.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodExecuteFunction(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.
Public methodExecuteFunction(Of 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. Returns a typed ObjectResult(Of T).
Public methodExecuteFunction(Of 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. Returns a typed ObjectResult(Of T).
Public methodExecuteStoreCommandExecutes an arbitrary command directly against the data source using the existing connection.
Public methodExecuteStoreQuery(Of TElement)(String, Object())Executes a query directly against the data source that returns a sequence of typed results.
Public methodExecuteStoreQuery(Of 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.
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodStatic memberGetKnownProxyTypesReturns all the existing proxy types.
Public methodGetObjectByKeyReturns an object that has the specified entity key.
Public methodStatic memberGetObjectTypeReturns the entity type of the POCO entity associated with a proxy object of a specified type.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodLoadProperty(Object, String)Explicitly loads an object related to the supplied object by the specified navigation property and using the default merge option.
Public methodLoadProperty(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.
Public methodLoadProperty(Of TEntity)(TEntity, Expression(Of Func(Of TEntity, Object)))Explicitly loads an object that is related to the supplied object by the specified LINQ query and by using the default merge option.
Public methodLoadProperty(Of TEntity)(TEntity, Expression(Of Func(Of TEntity, Object)), MergeOption)Explicitly loads an object that is related to the supplied object by the specified LINQ query and by using the specified merge option.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRefresh(RefreshMode, IEnumerable)Updates a collection of objects in the object context with data from the data source.
Public methodRefresh(RefreshMode, Object)Updates an object in the object context with data from the data source.
Public methodSaveChangesPersists all updates to the data source and resets change tracking in the object context.
Public methodSaveChanges(Boolean) Obsolete. Persists all updates to the data source and optionally resets change tracking in the object context.
Public methodSaveChanges(SaveOptions)Persists all updates to the data source with the specified SaveOptions.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodTranslate(Of TElement)(DbDataReader)Translates a DbDataReader that contains rows of entity data to objects of the requested entity type.
Public methodTranslate(Of TEntity)(DbDataReader, String, MergeOption)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.
Public methodTryGetObjectByKeyReturns an object that has the specified entity key.
Top

  NameDescription
Public eventObjectMaterializedOccurs when a new entity object is created from data in the data source as part of a query or load operation.
Public eventSavingChangesOccurs when changes are saved to the data source.
Top

NoteNote

The ObjectContext class is not thread safe. The integrity of data objects in an ObjectContext cannot be ensured in multithreaded scenarios.

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. An instance of the ObjectContext class encapsulates the following:

When 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.

This example is based on the Adventure Works Sales Model. The example shows you how to construct an ObjectContext.

' 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

.NET Framework

Supported in: 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft