Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ObjectContext Class
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
Public Class ObjectContext _
    Implements IDisposable
Visual Basic (Usage)
Dim instance As ObjectContext
C#
public class ObjectContext : IDisposable
Visual C++
public ref class ObjectContext : IDisposable
JScript
public class ObjectContext implements 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.

Visual Basic
' Create the ObjectContext.
Dim context As ObjectContext = _
    New ObjectContext("name=AdventureWorksEntities")

' Set the DefaultContainerName for the ObjectContext.
context.DefaultContainerName = "AdventureWorksEntities"

Dim query As ObjectQuery(Of Product) = _
    New ObjectQuery(Of Product)("Product", context)

' Iterate through the collection of Products.
Dim result As Product
For Each result In query
    Console.WriteLine("Product Name: {0}", result.Name)
Next
C#
// 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);
System..::.Object
  System.Data.Objects..::.ObjectContext
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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker