แก้ไข

แชร์ผ่าน


ObjectContext Constructors

Definition

Initializes a new instance of the ObjectContext class.

Overloads

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)

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

ObjectContext(EntityConnection, String)

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

ObjectContext(String, String)

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

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

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

Exceptions

The connection is null.

The connection is invalid.

-or-

The metadata workspace is invalid.

See also

Applies to

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

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

Exceptions

The connectionString is null.

The connectionString is invalid.

-or-

The metadata workspace is not valid.

Examples

This 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);

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

Applies to

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

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

defaultContainerName
String

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

Exceptions

The connection is null.

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

See also

Applies to

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

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

defaultContainerName
String

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

Exceptions

The connectionString is null.

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