ObjectContext Class

Definition

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

public class ObjectContext : IDisposable
Inheritance
ObjectContext
Implements

Examples

This 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);

Remarks

Note

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.

Constructors

ObjectContext(EntityConnection, String)

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

ObjectContext(EntityConnection)

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

ObjectContext(String, String)

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

ObjectContext(String)

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

Properties

CommandTimeout

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.

Connection

Gets the connection used by the object context.

ContextOptions

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

DefaultContainerName

Gets or sets the default container name.

MetadataWorkspace

Gets the metadata workspace used by the object context.

ObjectStateManager

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

QueryProvider

Gets the LINQ query provider associated with this object context.

Methods

AcceptAllChanges()

Accepts all changes made to objects in the object context.

AddObject(String, Object)

Adds an object to the object context.

ApplyCurrentValues<TEntity>(String, TEntity)

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

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

ApplyPropertyChanges(String, Object)
Obsolete.

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

Attach(IEntityWithKey)

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

AttachTo(String, Object)

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

CreateDatabase()

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

CreateDatabaseScript()

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.

CreateEntityKey(String, Object)

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

CreateObject<T>()

Creates and returns an instance of the requested type .

CreateObjectSet<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)

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>)

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

CreateQuery<T>(String, ObjectParameter[])

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

DatabaseExists()

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

DeleteDatabase()

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

DeleteObject(Object)

Marks an object for deletion.

Detach(Object)

Removes the object from the object context.

DetectChanges()

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

Dispose()

Releases the resources used by the object context.

Dispose(Boolean)

Releases the resources used by the object context.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
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[])

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

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. Returns a typed ObjectResult<T>.

ExecuteStoreCommand(String, Object[])

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

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[])

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

Serves as the default hash function.

(Inherited from Object)
GetKnownProxyTypes()

Returns all the existing proxy types.

GetObjectByKey(EntityKey)

Returns an object that has the specified entity key.

GetObjectType(Type)

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

GetType()

Gets the Type of the current instance.

(Inherited from Object)
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(Object, String)

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

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

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

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Refresh(RefreshMode, IEnumerable)

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

Refresh(RefreshMode, Object)

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

SaveChanges()

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

SaveChanges(Boolean)
Obsolete.

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

SaveChanges(SaveOptions)

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

ToString()

Returns a string that represents the current object.

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

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

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

TryGetObjectByKey(EntityKey, Object)

Returns an object that has the specified entity key.

Events

ObjectMaterialized

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

SavingChanges

Occurs when changes are saved to the data source.

Applies to

Product Versions
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also