ObjectContext Class

ObjectContext Class

.NET Framework (current version)
 

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

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

System.Object
  System.Data.Objects.ObjectContext

public class ObjectContext : IDisposable

NameDescription
System_CAPS_pubmethodObjectContext(EntityConnection)

Initializes a new instance of the ObjectContext class with the given connection. During construction, the metadata workspace is extracted from the EntityConnection object.

System_CAPS_protmethodObjectContext(EntityConnection, String)

Initializes a new instance of the ObjectContext class with a given connection and entity container name.

System_CAPS_pubmethodObjectContext(String)

Initializes a new instance of the ObjectContext class with the given connection string and default entity container name.

System_CAPS_protmethodObjectContext(String, String)

Initializes a new instance of the ObjectContext class with a given connection string and entity container name.

NameDescription
System_CAPS_pubpropertyCommandTimeout

Gets or sets the timeout value, in seconds, for all object context operations. A null value indicates that the default value of the underlying provider will be used.

System_CAPS_pubpropertyConnection

Gets the connection used by the object context.

System_CAPS_pubpropertyContextOptions

Gets the ObjectContextOptions instance that contains options that affect the behavior of the ObjectContext.

System_CAPS_pubpropertyDefaultContainerName

Gets or sets the default container name.

System_CAPS_pubpropertyMetadataWorkspace

Gets the metadata workspace used by the object context.

System_CAPS_pubpropertyObjectStateManager

Gets the object state manager used by the object context to track object changes.

System_CAPS_protpropertyQueryProvider

Gets the LINQ query provider associated with this object context.

NameDescription
System_CAPS_pubmethodAcceptAllChanges()

Accepts all changes made to objects in the object context.

System_CAPS_pubmethodAddObject(String, Object)

Adds an object to the object context.

System_CAPS_pubmethodApplyCurrentValues<TEntity>(String, TEntity)

Copies the scalar values from the supplied object into the object in the ObjectContext that has the same key.

System_CAPS_pubmethodApplyOriginalValues<TEntity>(String, 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.

System_CAPS_pubmethodApplyPropertyChanges(String, Object)

Obsolete.Applies property changes from a detached object to an object already attached to the object context.

System_CAPS_pubmethodAttach(IEntityWithKey)

Attaches an object or object graph to the object context when the object has an entity key.

System_CAPS_pubmethodAttachTo(String, Object)

Attaches an object or object graph to the object context in a specific entity set.

System_CAPS_pubmethodCreateDatabase()

Creates the database by using the current data source connection and the metadata in the StoreItemCollection.

System_CAPS_pubmethodCreateDatabaseScript()

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

System_CAPS_pubmethodCreateEntityKey(String, Object)

Creates the entity key for a specific object, or returns the entity key if it already exists.

System_CAPS_pubmethodCreateObject<T>()

Creates and returns an instance of the requested type .

System_CAPS_pubmethodCreateObjectSet<TEntity>()

Creates a new ObjectSet<TEntity> instance that is used to query, add, modify, and delete objects of the specified entity type.

System_CAPS_pubmethodCreateObjectSet<TEntity>(String)

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.

System_CAPS_pubmethodCreateProxyTypes(IEnumerable<Type>)

Generates an equivalent type that can be used with the Entity Framework for each type in the supplied enumeration.

System_CAPS_pubmethodCreateQuery<T>(String, ObjectParameter[])

Creates an ObjectQuery<T> in the current object context by using the specified query string.

System_CAPS_pubmethodDatabaseExists()

Checks if the database that is specified as the database in the current data source connection exists on the data source.

System_CAPS_pubmethodDeleteDatabase()

Deletes the database that is specified as the database in the current data source connection.

System_CAPS_pubmethodDeleteObject(Object)

Marks an object for deletion.

System_CAPS_pubmethodDetach(Object)

Removes the object from the object context.

System_CAPS_pubmethodDetectChanges()

Ensures that ObjectStateEntry changes are synchronized with changes in all objects that are tracked by the ObjectStateManager.

System_CAPS_pubmethodDispose()

Releases the resources used by the object context.

System_CAPS_protmethodDispose(Boolean)

Releases the resources used by the object context.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodExecuteFunction(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.

System_CAPS_pubmethodExecuteFunction<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<T>.

System_CAPS_pubmethodExecuteFunction<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<T>.

System_CAPS_pubmethodExecuteStoreCommand(String, Object[])

Executes an arbitrary command directly against the data source using the existing connection.

System_CAPS_pubmethodExecuteStoreQuery<TElement>(String, Object[])

Executes a query directly against the data source that returns a sequence of typed results.

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

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticGetKnownProxyTypes()

Returns all the existing proxy types.

System_CAPS_pubmethodGetObjectByKey(EntityKey)

Returns an object that has the specified entity key.

System_CAPS_pubmethodSystem_CAPS_staticGetObjectType(Type)

Returns the entity type of the POCO entity associated with a proxy object of a specified type.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodLoadProperty(Object, String)

Explicitly loads an object related to the supplied object by the specified navigation property and using the default merge option.

System_CAPS_pubmethodLoadProperty(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.

System_CAPS_pubmethodLoadProperty<TEntity>(TEntity, Expression<Func<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.

System_CAPS_pubmethodLoadProperty<TEntity>(TEntity, Expression<Func<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.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodRefresh(RefreshMode, IEnumerable)

Updates a collection of objects in the object context with data from the data source.

System_CAPS_pubmethodRefresh(RefreshMode, Object)

Updates an object in the object context with data from the data source.

System_CAPS_pubmethodSaveChanges()

Persists all updates to the data source and resets change tracking in the object context.

System_CAPS_pubmethodSaveChanges(Boolean)

Obsolete.Persists all updates to the data source and optionally resets change tracking in the object context.

System_CAPS_pubmethodSaveChanges(SaveOptions)

Persists all updates to the data source with the specified SaveOptions.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodTranslate<TElement>(DbDataReader)

Translates a DbDataReader that contains rows of entity data to objects of the requested entity type.

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

System_CAPS_pubmethodTryGetObjectByKey(EntityKey, Object)

Returns an object that has the specified entity key.

NameDescription
System_CAPS_pubeventObjectMaterialized

Occurs when a new entity object is created from data in the data source as part of a query or load operation.

System_CAPS_pubeventSavingChanges

Occurs when changes are saved to the data source.

System_CAPS_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 AdventureWorks Sales Model. The 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);

.NET Framework
Available since 3.5

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft