ObjectContext ObjectContext ObjectContext ObjectContext Constructors

Definition

Overloads

ObjectContext(EntityConnection) ObjectContext(EntityConnection) ObjectContext(EntityConnection) 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(String) ObjectContext(String) ObjectContext(String) ObjectContext(String)

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

ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String)

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

ObjectContext(String, String) ObjectContext(String, String) ObjectContext(String, String) ObjectContext(String, String)

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

ObjectContext(EntityConnection) ObjectContext(EntityConnection) ObjectContext(EntityConnection) 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.

public:
 ObjectContext(System::Data::EntityClient::EntityConnection ^ connection);
public ObjectContext (System.Data.EntityClient.EntityConnection connection);
new System.Data.Objects.ObjectContext : System.Data.EntityClient.EntityConnection -> System.Data.Objects.ObjectContext
Public Sub New (connection As EntityConnection)

Parameters

connection
EntityConnection EntityConnection EntityConnection EntityConnection

An EntityConnection that contains references to the model and to the data source connection.

Exceptions

The connection is invalid.

-or-

The metadata workspace is invalid.

See also

ObjectContext(String) ObjectContext(String) ObjectContext(String) ObjectContext(String)

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

public:
 ObjectContext(System::String ^ connectionString);
public ObjectContext (string connectionString);
new System.Data.Objects.ObjectContext : string -> System.Data.Objects.ObjectContext
Public Sub New (connectionString As String)

Parameters

connectionString
String String String String

The connection string, which also provides access to the metadata information.

Exceptions

The connectionString is invalid.

-or-

The metadata workspace is not valid.

Examples

The example in this topic is based on the AdventureWorks Sales Model. The example shows 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);
' Create the ObjectContext. 
Dim context As 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"

Dim query As ObjectSet(Of Product) = context.CreateObjectSet(Of Product)()

' Iterate through the collection of Products. 
For Each result As Product In query
    Console.WriteLine("Product Name: {0}", result.Name)
Next

Remarks

The connectionString is a string that contains information that is required to access a conceptual model and to connect to a data source. The EntityConnectionStringBuilder class can be used to correctly format the string.

The Entity Data Model tools generate a named connection string that is stored in the application's configuration file. This named connection string can be supplied instead of the connectionString parameter when instantiating the ObjectContext class.

See also

ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String) ObjectContext(EntityConnection, String)

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

protected:
 ObjectContext(System::Data::EntityClient::EntityConnection ^ connection, System::String ^ defaultContainerName);
protected ObjectContext (System.Data.EntityClient.EntityConnection connection, string defaultContainerName);
new System.Data.Objects.ObjectContext : System.Data.EntityClient.EntityConnection * string -> System.Data.Objects.ObjectContext
Protected Sub New (connection As EntityConnection, defaultContainerName As String)

Parameters

connection
EntityConnection EntityConnection EntityConnection EntityConnection

An EntityConnection that contains references to the model and to the data source connection.

defaultContainerName
String String String String

The name of the default entity container. When the defaultContainerName is set through this method, the property becomes read-only.

Exceptions

The connection, defaultContainerName, or metadata workspace is not valid.

See also

ObjectContext(String, String) ObjectContext(String, String) ObjectContext(String, String) ObjectContext(String, String)

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

protected:
 ObjectContext(System::String ^ connectionString, System::String ^ defaultContainerName);
protected ObjectContext (string connectionString, string defaultContainerName);
new System.Data.Objects.ObjectContext : string * string -> System.Data.Objects.ObjectContext
Protected Sub New (connectionString As String, defaultContainerName As String)

Parameters

connectionString
String String String String

The connection string, which also provides access to the metadata information.

defaultContainerName
String String String String

The name of the default entity container. When the defaultContainerName is set through this method, the property becomes read-only.

Exceptions

The connectionString, defaultContainerName, or metadata workspace is not valid.

Remarks

The connectionString is a string that contains information that is required to access a conceptual model and to connect to a data source. The EntityConnectionStringBuilder class can be used to correctly format the string.

See also

Applies to