ObjectContext Class

Definition

ObjectContext is the top-level object that encapsulates a connection between the CLR and the database, serving as a gateway for Create, Read, Update, and Delete operations.

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
public class ObjectContext : IDisposable, System.Data.Entity.Infrastructure.IObjectContextAdapter
type ObjectContext = class
    interface IDisposable
    interface IObjectContextAdapter
Public Class ObjectContext
Implements IDisposable, IObjectContextAdapter
Inheritance
ObjectContext
Attributes
Implements

Constructors

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(EntityConnection, Boolean)

Creates an ObjectContext with the given connection and metadata workspace.

ObjectContext(EntityConnection, String)

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

ObjectContext(String)

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

ObjectContext(String, String)

Initializes a new instance of the ObjectContext class with a given connection string and 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.

InterceptionContext

Returns the DbInterceptionContext being used for this context.

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.

TransactionHandler

Gets the transaction handler in use by this context. May be null if no transaction have been started.

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 store connection exists on the store. Most of the actual work is done by the DbProviderServices implementation for the current store connection.

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.

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, ExecutionOptions, ObjectParameter[])

Executes the given function on the default container.

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. The command is specified using the server's native query language, such as SQL.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreCommand("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreCommand("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreCommand(TransactionalBehavior, String, Object[])

Executes an arbitrary command directly against the data source using the existing connection. The command is specified using the server's native query language, such as SQL.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreCommand("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreCommand("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreCommandAsync(String, CancellationToken, Object[])

Asynchronously executes an arbitrary command directly against the data source using the existing connection. The command is specified using the server's native query language, such as SQL.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreCommandAsync(String, Object[])

Asynchronously executes an arbitrary command directly against the data source using the existing connection. The command is specified using the server's native query language, such as SQL.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreCommandAsync(TransactionalBehavior, String, CancellationToken, Object[])

Asynchronously executes an arbitrary command directly against the data source using the existing connection. The command is specified using the server's native query language, such as SQL.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreCommandAsync(TransactionalBehavior, String, Object[])

Asynchronously executes an arbitrary command directly against the data source using the existing connection. The command is specified using the server's native query language, such as SQL.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

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

Executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. Results are not tracked by the context, use the overload that specifies an entity set name to track results.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQuery<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQuery<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreQuery<TElement>(String, Object[])

Executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. Results are not tracked by the context, use the overload that specifies an entity set name to track results.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQuery<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQuery<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

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

Executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. If an entity set name is specified, results are tracked by the context.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQuery<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQuery<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

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

Executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. If an entity set name is specified, results are tracked by the context.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQuery<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQuery<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreQueryAsync<TElement>(String, CancellationToken, Object[])

Asynchronously executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. Results are not tracked by the context, use the overload that specifies an entity set name to track results.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreQueryAsync<TElement>(String, ExecutionOptions, CancellationToken, Object[])

Asynchronously executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. Results are not tracked by the context, use the overload that specifies an entity set name to track results.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreQueryAsync<TElement>(String, ExecutionOptions, Object[])

Asynchronously executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. Results are not tracked by the context, use the overload that specifies an entity set name to track results.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreQueryAsync<TElement>(String, Object[])

Asynchronously executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. Results are not tracked by the context, use the overload that specifies an entity set name to track results.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreQueryAsync<TElement>(String, String, ExecutionOptions, CancellationToken, Object[])

Asynchronously executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. If an entity set name is specified, results are tracked by the context.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

ExecuteStoreQueryAsync<TElement>(String, String, ExecutionOptions, Object[])

Asynchronously executes a query directly against the data source and returns a sequence of typed results. The query is specified using the server's native query language, such as SQL. If an entity set name is specified, results are tracked by the context.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.ExecuteStoreQueryAsync<Post>("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

Finalize()

Finalizes an instance of the ObjectContext class.

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.

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)

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

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)

Explicitly loads an object that is related to the supplied object by the specified LINQ query and by using the specified merge option.

Refresh(RefreshMode, IEnumerable)

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

Refresh(RefreshMode, Object)

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

RefreshAsync(RefreshMode, IEnumerable)

Asynchronously updates a collection of objects in the object context with data from the database.

RefreshAsync(RefreshMode, IEnumerable, CancellationToken)

Asynchronously updates a collection of objects in the object context with data from the database.

RefreshAsync(RefreshMode, Object)

Asynchronously updates an object in the object context with data from the database.

RefreshAsync(RefreshMode, Object, CancellationToken)

Asynchronously updates an object in the object context with data from the database.

SaveChanges()

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

SaveChanges(Boolean)
Obsolete.

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

SaveChanges(SaveOptions)

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

SaveChangesAsync()

Asynchronously persists all updates to the database and resets change tracking in the object context.

SaveChangesAsync(CancellationToken)

Asynchronously persists all updates to the database and resets change tracking in the object context.

SaveChangesAsync(SaveOptions)

Asynchronously persists all updates to the database and optionally resets change tracking in the object context.

SaveChangesAsync(SaveOptions, CancellationToken)

Asynchronously persists all updates to the database and optionally resets change tracking in the object context.

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.

Explicit Interface Implementations

IObjectContextAdapter.ObjectContext

Returns itself. ObjectContext implements IObjectContextAdapter to provide a common interface for DbContext and ObjectContext both of which will return the underlying ObjectContext.

Applies to