用于实体框架的 EntityClient 提供程序EntityClient Provider for the Entity Framework

EntityClient 提供程序是一种数据提供程序,实体框架应用程序使用该提供程序访问在概念模型中描述的数据。The EntityClient provider is a data provider used by Entity Framework applications to access data described in a conceptual model. 有关概念模型的信息,请参阅 建模和映射For information about conceptual models, see Modeling and Mapping. EntityClient 使用其他 .NET Framework 数据提供程序访问数据源。EntityClient uses other .NET Framework data providers to access the data source. 例如,EntityClient 在访问 SQL Server 数据库时使用 SQL Server .NET Framework 数据提供程序 (SqlClient)。For example, EntityClient uses the .NET Framework Data Provider for SQL Server (SqlClient) when accessing a SQL Server database. 有关 SqlClient 提供程序的信息,请参阅 SqlClient for the 实体框架For information about the SqlClient provider, see SqlClient for the Entity Framework. EntityClient 提供程序是在 System.Data.EntityClient 命名空间中实现的。The EntityClient provider is implemented in the System.Data.EntityClient namespace.

管理连接Managing Connections

实体框架通过 EntityConnection 向基础数据提供程序和关系数据库提供,在存储特定的 ADO.NET 数据提供程序的基础上建立。The Entity Framework builds on top of storage-specific ADO.NET data providers by providing an EntityConnection to an underlying data provider and relational database. 若要构造 EntityConnection 对象,你必须引用一组包含所需模型和映射的元数据,以及特定于存储的数据提供程序名称和连接字符串。To construct an EntityConnection object, you have to reference a set of metadata that contains the necessary models and mapping, and also a storage-specific data provider name and connection string. EntityConnection准备就绪后,可以通过从概念模型生成的类访问实体。After the EntityConnection is in place, entities can be accessed through the classes generated from the conceptual model.

可以在 app.config 文件中指定连接字符串。You can specify a connection string in app.config file.

System.Data.EntityClient 还包含 EntityConnectionStringBuilder 类。The System.Data.EntityClient also includes the EntityConnectionStringBuilder class. 通过使用此类的属性和方法,开发人员可以使用此类以编程方式创建语法正确的连接字符串,并可以分析和重新生成现有的连接字符串。This class enables developers to programmatically create syntactically correct connection strings, and parse and rebuild existing connection strings, by using properties and methods of the class. 有关详细信息,请参阅 如何:生成 EntityConnection 连接字符串For more information, see How to: Build an EntityConnection Connection String.

创建查询Creating Queries

Entity SQLEntity SQL语言是一种独立于存储的 SQL 方言,可直接处理概念实体架构,并支持实体数据模型的概念(例如继承和关系)。The Entity SQLEntity SQL language is a storage-independent dialect of SQL that works directly with conceptual entity schemas and supports Entity Data Model concepts such as inheritance and relationships. EntityCommand 类用于对实体模型执行 Entity SQLEntity SQL 命令。The EntityCommand class is used to execute an Entity SQLEntity SQL command against an entity model. 构造 EntityCommand 对象时,可以指定一个存储过程名称或查询文本。When you construct EntityCommand objects, you can specify a stored procedure name or a query text. 实体框架与存储特定的数据提供程序一起使用,以将泛型转换为 Entity SQLEntity SQL 特定于存储的查询。The Entity Framework works with storage-specific data providers to translate generic Entity SQLEntity SQL into storage-specific queries. 有关编写查询的详细信息 Entity SQLEntity SQL ,请参阅 实体 SQL 语言For more information about writing Entity SQLEntity SQL queries, see Entity SQL Language.

下面的示例创建一个 EntityCommand 对象,并 Entity SQLEntity SQL 将查询文本分配给其 EntityCommand.CommandText 属性。The following example creates an EntityCommand object and assigns an Entity SQLEntity SQL query text to its EntityCommand.CommandText property. Entity SQLEntity SQL 查询请求从概念模型中按标价排序的产品。This Entity SQLEntity SQL query requests products ordered by the list price from the conceptual model. 下面的代码完全不识别存储模型。The following code has no knowledge of the storage model at all.

EntityCommand cmd = conn.CreateCommand();
cmd.CommandText = @"SELECT VALUE p
 FROM AdventureWorksEntities.Product AS p
 ORDER BY p.ListPrice";

执行查询Executing Queries

执行查询时,查询将经过解析并转换为规范命令目录树。When a query is executed, it is parsed and converted into a canonical command tree. 所有后续处理都在该命令目录树上执行。All subsequent processing is performed on the command tree. 命令目录树是 System.Data.EntityClient 与基础 .NET Framework 数据访问接口(如)之间的通信方式 System.Data.SqlClientThe command tree is the means of communication between the System.Data.EntityClient and the underlying .NET Framework data provider, such as System.Data.SqlClient.

EntityDataReader 公开对概念模型执行 EntityCommand 的结果。The EntityDataReader exposes the results of executing a EntityCommand against a conceptual model. 若要执行返回 EntityDataReader 的命令,请调用 ExecuteReaderTo execute the command that returns the EntityDataReader, call ExecuteReader. EntityDataReader 实现 IExtendedDataRecord 以描述丰富结构化的结果。The EntityDataReader implements IExtendedDataRecord to describe rich structured results.

管理事务Managing Transactions

在实体框架中,有两种使用事务的方法:自动和显式。In the Entity Framework, there are two ways to use transactions: automatic and explicit. 自动事务使用 System.Transactions 命名空间,而显式事务使用 EntityTransaction 类。Automatic transactions use the System.Transactions namespace, and explicit transactions use the EntityTransaction class.

若要更新通过概念模型公开的数据,请参阅 如何:在实体框架中管理事务To update data that is exposed through a conceptual model, see How to: Manage Transactions in the Entity Framework.

本节内容In This Section

如何:生成 EntityConnection 连接字符串How to: Build an EntityConnection Connection String

如何:执行返回 PrimitiveType 结果的查询How to: Execute a Query that Returns PrimitiveType Results

如何:执行返回 StructuralType 结果的查询How to: Execute a Query that Returns StructuralType Results

如何:执行返回 RefType 结果的查询How to: Execute a Query that Returns RefType Results

如何:执行返回复杂类型的查询How to: Execute a Query that Returns Complex Types

如何:执行返回嵌套集合的查询How to: Execute a Query that Returns Nested Collections

如何:使用 EntityCommand 执行参数化实体 SQL 查询How to: Execute a Parameterized Entity SQL Query Using EntityCommand

如何:使用 EntityCommand 执行参数化存储过程How to: Execute a Parameterized Stored Procedure Using EntityCommand

如何:执行多态查询How to: Execute a Polymorphic Query

如何:使用导航运算符导航关系How to: Navigate Relationships with the Navigate Operator

请参阅See also