System.Data.EntityClient 命名空间是 实体框架的 .NET Framework 数据提供程序。EntityClient 提供程序使用存储特定的 ADO.NET 数据提供程序类和映射元数据与实体数据模型进行交互。EntityClient 首先将对概念性实体执行的操作转换为对物理数据源执行的操作。然后再将物理数据源返回的结果集转换为概念性实体。

EntityClient下的类有以下几个:

l           EntityConnection

l           EntityCommand

l           EntityConnectionStringBuilder

l           EntityParameter

l           EntityDataReader

l           EntityParameterCollection

l           EntityProviderFactory

l           EntityTransaction

从类的名字上看,我们就知道它们的作用是什么了。在此,就不再一一解释了。直接通过实例代码来学习它们。

l         EntityConnection:

实例代码1:

string con = "name = NorthwindEntities";

using (EntityConnection econn = new EntityConnection(con))

{

string esql = "Select VALUE c from NorthwindEntities.Customers as c where c.CustomerID='ALFKI'";

econn.Open();

EntityCommand ecmd = new EntityCommand(esql, econn);

EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess);

if (ereader.Read())

{

Console.WriteLine(ereader["CustomerID"]);

}

Console.WriteLine(ecmd.ToTraceString());

}

上述代码中,需要注意的是EntityConnection的构造方法。其中,连接字符串写法有多很,如下:

写法1:

string con ="name = NorthwindEntities" ;其中的”NorthwindEntities”是配置文件中的连接字符串名称

写法2:

string con = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindEntities"].ConnectionString; 其中的”NorthwindEntities”是配置文件中的连接字符串名称

写法3:

string con = @" metadata=res://*/NorthWind.csdl|res://*/NorthWind.ssdl|res://*/NorthWind.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True'";其中的这些字符串是配置文件中的连接字符串的值

写法4:

NorthwindEntities edm = new NorthwindEntities();

string con = edm.Connection.ConnectionString;

上述写法中,基于写法简单、方便我比较推荐使用第1种或者第2种写法。

l         EntityCommand

它具有的方法有:ExecuteDbDataReader、 ExecuteNonQuery 、 ExecuteReader 、 ExecuteScalar等。

实例代码2:

string con = "name = NorthwindEntities";

using (EntityConnection econn = new EntityConnection(con))

{

string esql = "Select VALUE c from NorthwindEntities.Customers as c where c.CustomerID='ALFKI'";

econn.Open();

EntityCommand ecmd = econn.CreateCommand();

ecmd.CommandText = esql;

EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess);

if (ereader.Read())

{

Console.WriteLine(ereader["CustomerID"]);

}

Console.WriteLine(ecmd.ToTraceString());

}

代码中,EntityCommand创建方式和实例代码1中的稍有不同,相信大家都明白,就不多说了。

l         EntityConnectionStringBuilder

实例代码3:

EntityConnectionStringBuilder esb = new EntityConnectionStringBuilder();

esb.Provider = "System.Data.SqlClient";

esb.Metadata = @"res://*/NorthWind.csdl|res://*/NorthWind.ssdl|res://*/NorthWind.msl";

esb.ProviderConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True";

EntityConnection econn = new EntityConnection(esb.ConnectionString)//创建连接

l         EntityParameter

代码实例4:

string con = "name = NorthwindEntities";

using (EntityConnection econn = new EntityConnection(con))

{

string esql = "Select value c from NorthwindEntities.Customers as c order by c.CustomerID skip @start limit @end";

econn.Open();

EntityCommand ecmd = new EntityCommand(esql, econn);

EntityParameter p1 = new EntityParameter("start", DbType.Int32);

p1.Value = 0;

EntityParameter p2 = new EntityParameter("end", DbType.Int32);

p2.Value = 10;

ecmd.Parameters.Add(p1);

ecmd.Parameters.Add(p2);

EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess);

while (ereader.Read())

{

Console.WriteLine(ereader["CustomerID"]);

}

Console.WriteLine(ecmd.ToTraceString());

}

其中,参数是以@符号前缀的,EntityParameter实体参数类,除了可以直接构造出实例来。为实体命令对象添加参数,我们还可以直接调用Parameters.AddWithValue方法。如下代码:

ecmd.Parameters.AddWithValue("start", 0);

ecmd.Parameters.AddWithValue("end", 10);

我比较喜欢用上面的代码,简单、方便。

l         EntityDataReader

string con = "name = NorthwindEntities";

using (EntityConnection econn = new EntityConnection(con))

{

string esql = "Select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10 ";

econn.Open();

EntityCommand ecmd = new EntityCommand(esql, econn);

EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess);

while (ereader.Read())

{

Console.WriteLine("{0},{1},{2},{3},{4}", ereader[0], ereader[1], ereader[2], ereader[3], ereader[4]);

}

Console.WriteLine(ecmd.ToTraceString());

}

需要注意的是:CommandBehavior.SequentialAccess;这个地方。不同的枚举项,对查询会有不同影响。枚举如下:

l           Default 此查询可能返回多个结果集。在功能上等效于调用 ExecuteReader()。

l           SingleResult 查询返回一个结果集。

l           SchemaOnly 查询仅返回列信息。

l           KeyInfo 此查询返回列和主键信息。

l           SingleRow 查询应返回一行。

l           SequentialAccess 提供一种方法,以便 DataReader 处理包含带有大二进制值的列的行。

l           CloseConnection 在执行该命令时,如果关闭关联的 DataReader 对象,则关联的 Connection 对象也将关闭。

需要说明的是,如果使用SequentialAccess则需按顺序访问列,否则将抛异常。如下代码,将会抛异常:

while (ereader.Read())

{

//异常信息:从列序列号“1”开始读取的尝试无效。通过CommandBehavior.SequentialAccess,只能从列序列号“5”或更大值处开始读取

Console.WriteLine("{0},{1},{2},{3},{4}", ereader[4], ereader[1], ereader[2], ereader[3], ereader[0]);

}

l         EntityTransaction:

事务类。目前由于ESQL仅提供查询的命令,没有提供对Insert、Update、Delete等的支持。所以,我觉得目前这个类基本没有用,(不可能我做查询还使用事务吧!)。

从上述简单的介绍,我们可以看到,EntityClient和SqlClient下的类基本上是一致的。所以很容易掌握。其他就不多说了。

Entity Framework 学习初级篇6--EntityClient的更多相关文章

  1. Entity Framework学习初级篇2

    Entity Framework 学习初级篇2--ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager类的介绍 本节,简单的介绍E ...

  2. Entity Framework 学习初级篇--EntityClient(转)

    摘自:http://www.cnblogs.com/xray2005/archive/2009/05/13/1456374.html System.Data.EntityClient 命名空间是 实体 ...

  3. Entity Framework 学习初级篇--基本操作:增加、更新、删除、事务(转)

    摘自:http://www.cnblogs.com/xray2005/archive/2009/05/17/1458568.html 本节,直接写通过代码来学习.这些基本操作都比较简单,与这些基本操作 ...

  4. Entity Framework 学习初级篇7--基本操作:增加、更新、删除、事务

    本节,直接写通过代码来学习.这些基本操作都比较简单,与这些基本操作相关的内容在之前的1至6节基本介绍完毕. l           增加: 方法1:使用AddToXXX(xxx)方法:实例代码如下: ...

  5. Entity Framework 学习初级篇1--EF基本概况

    转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685011.html 最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不 ...

  6. Entity Framework学习初级篇1--EF基本概况《转》

    最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不是很完善,半成品.不过,据说在.Net4.0中,微软将推荐使用此框架,并会有所改善.而且,现在基本上所有数据库均提供了对EF的支 ...

  7. Entity Framework 学习初级篇2--ObjectContext类的介绍

    转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685014.html 本节,简单的介绍EF中的ObjectContext.ObjectQuer ...

  8. Entity Framework 学习初级篇4--Entity SQL

    Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM).Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询 ...

  9. Entity Framework 学习初级篇2--ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager类的介绍

    本节,简单的介绍EF中的ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager这个几个比较重要的类,它们都位于System.Data ...

随机推荐

  1. ASP.NET应用程序的生命周期

    对基于.Net平台的Web应用程序开发者来说,编写适合的应用程序生命周期的代码是非常重要的.所以必须要了解应用程序生命周期才能在适当的生命周期阶段编写合适代码,达到预期的效果.ASP.NET应用程序的 ...

  2. wordpress 修改过程

    chown -R www /home/www/wordpress,把所有者修改成www. 如果不行 则 解决"要执行请求的操作,WordPress需要访问您网页服务器的权限"方法: ...

  3. iOStextView的代理方法展示

    UITextView的代理方法 textViewShouldBeginEditing: and textViewDidBeginEditing: - (BOOL)textViewShouldBegin ...

  4. 《剑指Offer》算法题——二维数组查找

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. class Solutio ...

  5. linux脚本Shell之awk详解(二)

    三.printf的使用   print format 生成报表 %d        十进制有符号整数 %u        十进制无符号整数 %f        浮点数 %s        字符串 %c ...

  6. linux中tar 打包指定路径文件

    linux中tar打包指定路径文件www.111cn.net 编辑:yahoo 来源:转载在linux系统中打包与解压文件我都可以使用tar命令来解决,只要使用不同的参数就可以实现不同的需要了,下面来 ...

  7. Broken Keyboard(悲剧文本)

    你有一个键盘,键盘上所有的键都能正常使用,只是Home键和End键有时会自动按下.你并不知道这一情况,而是专心地打稿子,甚至连显示器都没开电源.当你打开显示器之后,展现在你面前的是一段悲剧文本.你的任 ...

  8. LeetCode OJ 236. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  9. implement a system call in minix

    http://www.papervisions.com/implementing-system-call-in-minix-os/

  10. nginx配置错误

    重启nginx:sudo /usr/local/nginx/sbin/nginx -s reload 出现错误提示:nginx: [emerg] unknown directive "if& ...