通过


ObjectContext 构造函数

定义

初始化 ObjectContext 类的新实例。

重载

名称 说明
ObjectContext(EntityConnection)

使用给定的连接初始化类的新实例 ObjectContext 。 在构造过程中,元数据工作区将从 EntityConnection 对象中提取。

ObjectContext(String)

使用给定的连接字符串和默认实体容器名称初始化 ObjectContext 类的新实例。

ObjectContext(EntityConnection, String)

使用给定的连接和实体容器名称初始化类的新实例 ObjectContext

ObjectContext(String, String)

使用给定连接字符串和实体容器名称初始化 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)

使用给定连接字符串和实体容器名称初始化 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 类可用于正确设置字符串的格式。

另请参阅

适用于