Working with Object Context in the ADO.NET Entity Framework : Page 2

Take advantage of the Object Context in Entity Framework to perform CRUD operations and attach or detach entities.

by Joydip Kanjilal

Jan 20, 2012

Page 2 of 3

Working with the Object Context

The Object Context in Entity Framework (like as in LINQ to SQL) is the gateway to execute your queries against the Entity Data Model. Any object that is returned as a result of a query execution is attached to the Object Context. The Object Context can in turn track changes to the object and also persist the object to the data store.

The Object Context interacts with the database and abstracts the way the connection string, the connection to the underlying database, the queries, and the stored procedures are executed. It also manages reads and writes to and from the database.

The Object Context uses the ObjectStateManager to manage the state changes of objects and then applies those changes to the underlying data store appropriately. The Object Context in Entity Framework is represented by the class called ObjectContext.

CRUD Operations Using the ObjectContext

You can use the Object Context to perform CRUD operations on your data exposed by the Entity Data Model. Here's how:

Attaching and Detaching Objects from the Object Context

You can attach or detach objects to and from the Object Context using methods such as, Attach() or Detach(). To attach a previously detached object to the Object Context, you can use the following code: