ObjectContext Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
- Inheritance
-
ObjectContext
- Implements
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);
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:
A connection to the database, in the form of an EntityConnection object.
Metadata that describes the model, in the form of a MetadataWorkspace object.
An ObjectStateManager object that manages objects persisted in the cache.
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.
Object |
Initializes a new instance of the ObjectContext class with a given connection and entity container name. |
Object |
Initializes a new instance of the ObjectContext class with the given connection. During construction, the metadata workspace is extracted from the EntityConnection object. |
Object |
Initializes a new instance of the ObjectContext class with a given connection string and entity container name. |
Object |
Initializes a new instance of the ObjectContext class with the given connection string and default entity container name. |
Command |
Gets or sets the timeout value, in seconds, for all object context operations. A |
Connection |
Gets the connection used by the object context. |
Context |
Gets the ObjectContextOptions instance that contains options that affect the behavior of the ObjectContext. |
Default |
Gets or sets the default container name. |
Metadata |
Gets the metadata workspace used by the object context. |
Object |
Gets the object state manager used by the object context to track object changes. |
Query |
Gets the LINQ query provider associated with this object context. |
Accept |
Accepts all changes made to objects in the object context. |
Add |
Adds an object to the object context. |
Apply |
Copies the scalar values from the supplied object into the object in the ObjectContext that has the same key. |
Apply |
Copies the scalar values from the supplied object into set of original values for the object in the ObjectContext that has the same key. |
Apply |
Obsolete.
Applies property changes from a detached object to an object already attached to the object context. |
Attach(IEntity |
Attaches an object or object graph to the object context when the object has an entity key. |
Attach |
Attaches an object or object graph to the object context in a specific entity set. |
Create |
Creates the database by using the current data source connection and the metadata in the StoreItemCollection. |
Create |
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. |
Create |
Creates the entity key for a specific object, or returns the entity key if it already exists. |
Create |
Creates and returns an instance of the requested type . |
Create |
Creates a new ObjectSet<TEntity> instance that is used to query, add, modify, and delete objects of the specified entity type. |
Create |
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. |
Create |
Generates an equivalent type that can be used with the Entity Framework for each type in the supplied enumeration. |
Create |
Creates an ObjectQuery<T> in the current object context by using the specified query string. |
Database |
Checks if the database that is specified as the database in the current data source connection exists on the data source. |
Delete |
Deletes the database that is specified as the database in the current data source connection. |
Delete |
Marks an object for deletion. |
Detach(Object) |
Removes the object from the object context. |
Detect |
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) |
Execute |
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. |
Execute |
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>. |
Execute |
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>. |
Execute |
Executes an arbitrary command directly against the data source using the existing connection. |
Execute |
Executes a query directly against the data source that returns a sequence of typed results. |
Execute |
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. |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Returns all the existing proxy types. |
Get |
Returns an object that has the specified entity key. |
Get |
Returns the entity type of the POCO entity associated with a proxy object of a specified type. |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Load |
Explicitly loads an object that is related to the supplied object by the specified navigation property and using the specified merge option. |
Load |
Explicitly loads an object related to the supplied object by the specified navigation property and using the default merge option. |
Load |
Explicitly loads an object that is related to the supplied object by the specified LINQ query and by using the specified merge option. |
Load |
Explicitly loads an object that is related to the supplied object by the specified LINQ query and by using the default merge option. |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
Refresh(Refresh |
Updates a collection of objects in the object context with data from the data source. |
Refresh(Refresh |
Updates an object in the object context with data from the data source. |
Save |
Persists all updates to the data source and resets change tracking in the object context. |
Save |
Obsolete.
Persists all updates to the data source and optionally resets change tracking in the object context. |
Save |
Persists all updates to the data source with the specified SaveOptions. |
To |
Returns a string that represents the current object. (Inherited from Object) |
Translate<TElement>(Db |
Translates a DbDataReader that contains rows of entity data to objects of the requested entity type. |
Translate<TEntity>(Db |
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. |
Try |
Returns an object that has the specified entity key. |
Object |
Occurs when a new entity object is created from data in the data source as part of a query or load operation. |
Saving |
Occurs when changes are saved to the data source. |
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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: