ADO.NET Entity Framework 入门示例向导(附Demo程序下载)- 系列2
本篇文章在《
ADO.NET Entity Framework 入门示例向导(附Demo程序下载)》基础上,进一步演示如何使用EntityClient 新数据提供程序、对象服务(Object Services)和LINQ to Entities与概念模型交互。Entity Framework 使用概念层、映射层和逻辑层将关系数据库结构抽象化。EntityClient和Entity SQL(新语言-实体SQL)可以与概念层的实体数据模型(Entity Data Model - EDM)交互。
如下是Entity Framework 组件图:
下面分别演示如何使EntityClient、对象服务Object Services、LINQ to Entities 访问概念数据模型。
1.使用EntityClient
EntityClient 是新的.NET 数据提供程序,EntityClient使用基于文本的语言Entity SQL与概念模型通信。
EntityClient 中的类与常见的 ADO.NET 提供程序中的类相似。例如,使用 EntityCommand 对象执行 EntityClient 查询,这需要 EntityConnection 对象连接到 EDM。当 EntityClient 与 EDM 中的实体交互时,EntityClient 不返回实体的实例而返回 DbDataReader 对象中的所有结果。EntityClient 可以返回一组标准行和列,也可以通过 DbDataReader 返回更复杂的分层数据的表示形式。
示例代码:
string customerID = txtCustomerID.Text.Trim();
// Contains a reference to an Entity Data Model (EDM) and a data source connection.
using (EntityConnection cn = new EntityConnection("Name=NorthwindEntities"))
{
cn.Open();
EntityCommand cmd = cn.CreateCommand();
cmd.CommandText =
"SELECT VALUE c FROM NorthwindEntities.Customers "+
"AS c WHERE c.CustomerID = @customerID";
cmd.Parameters.AddWithValue("customerID", customerID);
DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while (rdr.Read())
Console.WriteLine(rdr["CompanyName"].ToString());
rdr.Close();
}
示例使用 EntityClient 连接到概念模型并检索特定的客户。EntityConnection 可以接受概念层的完整连接字符串或 App.Config 文件中连接字符串的名称。连接字符串包含元数据文件(CSDL、MSL 和 SSDL 文件)列表,以及存储的专用于数据库的连接字符串信息。
如下是示例程序使用的数据库连接串:
<connectionStrings>
<add name="NorthwindEntities" connectionString=" metadata= res://NorthwindEDM/NorthwindModel.csdl|res://NorthwindEDM/NorthwindModel.ssdl|res://NorthwindEDM/NorthwindModel.msl; provider=System.Data.SqlClient;provider connection string=" Data Source=localhost; Initial Catalog=Northwind;Integrated Security=True; MultipleActiveResultSets=True""providerName="System.Data.EntityClient" />
</connectionStrings>
2.使用对象服务Object Services
与由 EDM 表示的数据进行交互的另一种方法是使用对象服务Object Services,对象服务允许直接返回对象列表。
下面的示例演示了如何使用对象服务和实体 SQL 进行查询以检索 Customers 列表:
NorthwindEntities northwindContext = new NorthwindEntities();
string customerID = txtCustomerID.Text.Trim();
ObjectQuery<Customers> query = northwindContext.CreateQuery<Customers>(
"SELECT VALUE c FROM Customers AS c WHERE c.CustomerID = @customerID",
new ObjectParameter("customerID", customerID));
foreach (Customers c in query)
Console.WriteLine(c.CustomerID + "---" + c.CompanyName);
在 EDM 中,EntityContainer 由从 ObjectContext(在本示例中为 northwindContext)继承的类表示。ObjectContext 类实施 ObjectQuery<T> 接口,从而使其可以使用实体 SQL 或 LINQ 创建查询。
CreateQuery 方法接受参数化的实体 SQL 语句,该语句定义了将检索 Customers 实体列表的查询。通过使用 foreach 语句对 ObjectQuery<Customers> 进行迭代时将执行作用于数据库的实际 SQL 语句。
3. 使用 LINQ to Entities
上述实体SQL脚本可以通过如下的LINQ to Entities 脚本实现,代码如下:
NorthwindEntities northwindContext = new NorthwindEntities();
string customerID = txtCustomerID.Text.Trim();
var query = from c in northwindContext.Customers
where c.CustomerID == customerID
select c;
foreach (Customers c in query)
Console.WriteLine(c.CustomerID + "---" + c.CompanyName);
本示例程序演示界面如下:
使用实体框架Entity Framework,开发人员可以通过对象模型(而不是逻辑/关系数据模型)专注于数据。一旦完成 EDM 的设计并将其映射到关系存储后,就可以使用 EntityClient、ObjectServices 和 LINQ 等多种技术与对象交互。
下载示例代码和项目。

EntityFrameworkDemo_Part2.rar 176 KB, 下载 808 次.