ObjectContext 建構函式

定義

初始化 ObjectContext 類別的新執行個體。

多載

ObjectContext(EntityConnection)

使用給定的連接,初始化 ObjectContext 類別的新執行個體。 在建構期間,系統會從 EntityConnection 物件中擷取中繼資料工作空間。

ObjectContext(String)

使用給定的連接字串和預設實體容器名稱,初始化 ObjectContext 類別的新執行個體。

ObjectContext(EntityConnection, String)

使用給定的連接和實體容器名稱,初始化 ObjectContext 類別的新執行個體。

ObjectContext(String, String)

使用給定的連接字串 (Connection String) 和實體容器 (Container) 名稱,初始化 ObjectContext 類別的新執行個體。

ObjectContext(EntityConnection)

使用給定的連接,初始化 ObjectContext 類別的新執行個體。 在建構期間,系統會從 EntityConnection 物件中擷取中繼資料工作空間。

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)

參數

connection
EntityConnection

EntityConnection,其中包含模型的參考與資料來源連接的參考。

例外狀況

connectionnull

connection 無效。

-或-

中繼資料工作空間無效。

另請參閱

適用於

ObjectContext(String)

使用給定的連接字串和預設實體容器名稱,初始化 ObjectContext 類別的新執行個體。

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)

參數

connectionString
String

連接字串,它也會提供中繼資料資訊的存取權。

例外狀況

connectionStringnull

connectionString 無效。

-或-

中繼資料工作空間無效。

範例

此範例示範如何建構 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);

備註

connectionString是字串,其中包含存取概念模型和聯機至數據源所需的資訊。 EntityConnectionStringBuilder 類別可用來正確格式化此字串。

實體數據模型工具會產生儲存在應用程式組態檔中的具名 連接字串。 這個具名 連接字串 可以在具現化 ObjectContext 類別時提供,而不是 connectionString 參數。

另請參閱

適用於

ObjectContext(EntityConnection, String)

使用給定的連接和實體容器名稱,初始化 ObjectContext 類別的新執行個體。

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)

參數

connection
EntityConnection

EntityConnection,其中包含模型的參考與資料來源連接的參考。

defaultContainerName
String

預設實體容器的名稱。 當您透過這個方法設定 defaultContainerName 時,此屬性會變成唯讀的。

例外狀況

connectionnull

connectiondefaultContainerName 或中繼資料工作區無效。

另請參閱

適用於

ObjectContext(String, String)

使用給定的連接字串 (Connection String) 和實體容器 (Container) 名稱,初始化 ObjectContext 類別的新執行個體。

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)

參數

connectionString
String

連接字串,它也會提供中繼資料資訊的存取權。

defaultContainerName
String

預設實體容器的名稱。 當您透過這個方法設定 defaultContainerName 時,此屬性會變成唯讀的。

例外狀況

connectionStringnull

connectionStringdefaultContainerName 或中繼資料工作區無效。

備註

connectionString是字串,其中包含存取概念模型和聯機至數據源所需的資訊。 EntityConnectionStringBuilder 類別可用來正確格式化此字串。

另請參閱

適用於