This documentation is archived and is not being maintained.

ObjectContext Class

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

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

public class ObjectContext : IDisposable

The ObjectContext class is the primary class for interacting with data as objects that are instances of entity types that are defined in an Entity Data Model (EDM). An instance of the ObjectContext class encapsulates the following:

When the object layer that represents the conceptual model of an EDM is generated by the Entity Data Model tools, the class that represents the EntityContainer for the model is derived from the ObjectContext. For more information, see Object Services Overview (Entity Framework).

This example is based on the Adventure Works 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.
context.DefaultContainerName = "AdventureWorksEntities";

ObjectQuery<Product> query = new ObjectQuery<Product>("Product", context);

// Iterate through the collection of Products. 
foreach (Product result in query)
    Console.WriteLine("Product Name: {0}", result.Name);


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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1