1,本篇接着上篇link to sql的实例扩充entity sql.
Entity sql
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Xml.Linq; using System.Data.Objects; using System.Data.Common; namespace Console1 { class Program { static void Main(string[] args) { //创建数据库实体 using (studentEntities stuEntity = new studentEntities()) { #region Execute方法 //string esql = "select value c from studentEntities.T_StuInfo as c order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query = stuEntity.CreateQuery<T_StuInfo>(esql);//创建查询对象 //foreach (var c in query) //{ // Console.WriteLine(c.stuid); //} //ObjectResult<T_StuInfo> results = query.Execute(MergeOption.NoTracking);//执行对象查询 //Console.WriteLine(results.Count()); #endregion #region GetResultType方法:返回查询结果的类型信息 //string esql = "select value c from studentEntities.T_StuInfo as c order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query = stuEntity.CreateQuery<T_StuInfo>(esql); //Console.WriteLine(query.GetResultType().ToString()); #endregion #region ToTraceString方法:获取当前执行的SQL语句。 //string esql = "select value c from studentEntities.T_StuInfo as c"; //ObjectQuery<T_StuInfo> query1 = stuEntity.CreateQuery<T_StuInfo>(esql); ////使用ObjectParameter的写法 //query1 = query1.Where("it.stuid=@stuid"); //增加参数 //query1.Parameters.Add(new ObjectParameter("stuid", 4));//为参数赋值 ////也可以这样写 //ObjectQuery<T_StuInfo> query2 = stuEntity.T_StuInfo.Where("it.stuid=4"); //foreach (var c in query1) // Console.WriteLine(c.stuid); ////显示查询执行的sql语句 //Console.WriteLine(query1.ToTraceString()); //Console.WriteLine(query2.ToTraceString()); #endregion #region First/ FirstOrDefault //string esql = "select value c from studentEntities.T_StuInfo as c order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query = stuEntity.CreateQuery<T_StuInfo>(esql).Where("it.stuid =4"); //T_StuInfo stu1 = query.First(); //如果要查询的不存在,异常:序列不包含任何元素 //T_StuInfo stu2 = query.FirstOrDefault(); //Console.WriteLine(stu1.stuid); //Console.WriteLine(stu2.stuid); #endregion #region Distinct //string esql = "select value c.name from studentEntities.T_StuInfo as c order by c.stuid limit 10"; //ObjectQuery<string> query = stuEntity.CreateQuery<string>(esql); //query = query.Distinct(); //foreach (string c in query) //{ // Console.WriteLine("姓名:{0}", c); //} #endregion #region Except:返回两个查询的差集 //string esql1 = "select value c from studentEntities.T_StuInfo as c order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query1 = stuEntity.CreateQuery<T_StuInfo>(esql1); //string esql2 = "select value c from studentEntities.T_StuInfo as c where c.stuid > 8 order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query2 = stuEntity.CreateQuery<T_StuInfo>(esql2); //query1 = query1.Except(query2);//将query1中的query2减去 //foreach(var c in query1) //{ // Console.WriteLine("学号:{0},姓名:{1}",c.stuid,c.name); //} #endregion #region Intersect:返回两个查询的交集 //string esql1 = "select value c from studentEntities.T_StuInfo as c order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query1 = stuEntity.CreateQuery<T_StuInfo>(esql1); //string esql2 = "select value c from studentEntities.T_StuInfo as c where c.stuid < 7 order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query2 = stuEntity.CreateQuery<T_StuInfo>(esql2); //query1 = query1.Intersect(query2);//创建交集 //foreach (var c in query1) //{ // Console.WriteLine(c.name); //} #endregion #region Include:可通过此方法查询出与相关的实体对象 //Union/UnionAll:返回两个查询的合集,包括重复项。其中UnionAll必须是相同类型或者是可以相互转换的。 //string esql1 = "select value c from studentEntities.T_StuInfo as c where c.stuid = 4"; //ObjectQuery<T_StuInfo> query1 = stuEntity.CreateQuery<T_StuInfo>(esql1); //query1 = query1.Include("T_Class");//查询结果中包含与之相关的实体对象 //foreach (T_StuInfo c in query1) //{ // Console.WriteLine("姓名:{0},班级:{1}", c.name, c.T_Class.name); //} #endregion #region orderby //string esql1 = "select value c from studentEntities.T_StuInfo as c order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query1 = stuEntity.CreateQuery<T_StuInfo>(esql1); //query1.OrderBy("it.name asc,it.stuid asc"); ////也可以这样写 ////query1.OrderBy("it.name desc"); ////query1.OrderBy("it.stuid asc"); //foreach (var c in query1) //{ // Console.WriteLine("姓名:{0},学号:{1}", c.name, c.stuid); //} #endregion #region Select SelectValue //string esql1 = "select value c from studentEntities.T_StuInfo as c order by c.stuid limit 10"; //ObjectQuery<T_StuInfo> query1 = stuEntity.CreateQuery<T_StuInfo>(esql1); //ObjectQuery<DbDataRecord> records = query1.Select("it.stuid,it.name"); //ObjectQuery<string> records2 = query1.SelectValue<string>("it.name"); //foreach (string c in records2) //{ // Console.WriteLine("{0}", c); //} //Console.WriteLine(records2.ToTraceString()); //foreach (DbDataRecord c in records) //{ // Console.WriteLine("{0},{1}", c[0], c[1]); //} //Console.WriteLine(records.ToTraceString()); #endregion #region Skip/Top string esql1 = "select value c from studentEntities.T_StuInfo as c order by c.stuid"; ObjectQuery<T_StuInfo> query1 = stuEntity.CreateQuery<T_StuInfo>(esql1); query1 = query1.Skip("it.stuid asc", "2"); query1 = query1.Top("10"); foreach (T_StuInfo c in query1) { Console.WriteLine("学号:{0},姓名:{1}", c.stuid, c.name); } Console.WriteLine(query1.ToTraceString()); //输出结构如下: /* SELECT TOP (10) [Extent1].[stuid] AS [stuid], [Extent1].[name] AS [name], [Extent1].[phone] AS [phone], [Extent1].[classid] AS [classid] FROM ( SELECT [Extent1].[stuid] AS [stuid], [Extent1].[name] AS [name], [Extent1 ].[phone] AS [phone], [Extent1].[classid] AS [classid], row_number() OVER (ORDER BY [Extent1].[stuid] ASC) AS [row_number] FROM [dbo].[T_StuInfo] AS [Extent1] ) AS [Extent1] WHERE [Extent1].[row_number] > 2 ORDER BY [Extent1].[stuid] ASC */ #endregion Console.WriteLine("OK"); Console.Read(); } } } }
2.下面是ef的增删改查
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Xml.Linq; using System.Data.Objects; using System.Data.Common; using System.Data.EntityClient; using System.Data; using System.Configuration; namespace Console1 { class Program { static void Main(string[] args) { using (studentEntities stuEntity = new studentEntities()) { #region 增加:使用AddTo表(xxx)方法 //T_StuInfo stu1 = new T_StuInfo { name = "wangjinhe1",phone ="123456",classid = 2 }; //stuEntity.AddToT_StuInfo(stu1);//直接添加到表对象中 //int resutl1 = stuEntity.SaveChanges();//保存修改 //Console.WriteLine(resutl1); //T_StuInfo stu2 = stuEntity.T_StuInfo.FirstOrDefault(c => c.name == "wangjinhe1"); //Console.WriteLine("id:{0},name:{1},phone:{2},classid:{3}", stu2.stuid, stu2.name, stu2.phone, stu2.classid); #endregion #region 增加:使用ObjectContext的AddObject(string entitySetName, object entity) //T_StuInfo stu1 = new T_StuInfo { name = "wangjinhe2",phone ="123456",classid = 2 }; //stuEntity.AddObject("T_StuInfo", stu1); //int resutl1 = stuEntity.SaveChanges();//保存修改 //Console.WriteLine(resutl1); //T_StuInfo stu2 = stuEntity.T_StuInfo.FirstOrDefault(c => c.name == "wangjinhe2"); //Console.WriteLine("id:{0},name:{1},phone:{2},classid:{3}", stu2.stuid, stu2.name, stu2.phone, stu2.classid); ////注意:AddObject方法中参数“entitySetName ”就是指对应实体名称,应该是:“T_StuInfo”,而不是“studentEntities.T_StuInfo”; #endregion #region 更新1:先取出来,再更新,然后保存,两次数据库连接,效率低 //T_StuInfo stu1 = stuEntity.T_StuInfo.FirstOrDefault(c => c.name == "wangjinhe1"); //stu1.name = "wangjinhe11"; //取出来后直接修改 //stu1.phone = "121212121"; //stu1.classid = 2; //stuEntity.SaveChanges(); //T_StuInfo stu2 = stuEntity.T_StuInfo.FirstOrDefault(c => c.name == "wangjinhe11"); //Console.WriteLine("id:{0},name:{1},phone:{2},classid:{3}", stu2.stuid, stu2.name, stu2.phone, stu2.classid); #endregion #region 直接更新 :通过ObjectStateManage来控制添加、修改、删除队列以及实体的状态 //T_StuInfo stu1 = new T_StuInfo();//假设这是DTO(数据传输对象)传递过来的对象 //stu1.stuid = 2; //需要指明主键,否则报错 //stu1.name = "西门春雪"; //stu1.phone = "18790186666"; //stu1.classid = 2; //stuEntity.T_StuInfo.Attach(stu1);//将对象附加到对象上下文中 //stuEntity.ObjectStateManager.ChangeObjectState(stu1, EntityState.Modified);//修改状态 //int result = stuEntity.SaveChanges();//保存到数据库,这样仅仅有一次连接数据库 //Console.WriteLine(result); //stu1 = stuEntity.T_StuInfo.FirstOrDefault(c => c.stuid == 2);//取出来查看是否已经修改 //Console.WriteLine("学号:{0},姓名:{1},电话:{2},班级:{3}", stu1.stuid, stu1.name, stu1.phone, stu1.T_Class.name); #endregion #region 删除 //T_StuInfo stu1 = stuEntity.T_StuInfo.FirstOrDefault(cc => cc.name == "王金河8"); //stuEntity.DeleteObject(stu1);//删除对象 //int result = stuEntity.SaveChanges(); //Console.WriteLine(result); //T_StuInfo stu2 = stuEntity.T_StuInfo.FirstOrDefault(cc => cc.name == "王金河8"); //if (stu2 == null) //{ // Console.WriteLine("您查找的数据已经删除"); //} #endregion } #region 事务 //studentEntities stuEntity2 = null; //数据库对象为空 //System.Data.Common.DbTransaction tran = null; //事务对象为空 //try //{ // stuEntity2 = new studentEntities();//新建数据库对象 // stuEntity2.Connection.Open(); // tran = stuEntity2.Connection.BeginTransaction();//开始事务 // T_StuInfo stu1 = stuEntity2.T_StuInfo.FirstOrDefault(cc => cc.name == "王金河7"); // stu1.phone = "13507611111"; // stu1.classid = 2; // stuEntity2.SaveChanges();//保存更改 // tran.Commit();//提交事务 //} //catch(Exception ex) //{ // if (tran != null) // tran.Rollback();//回滚事务 // throw ex; //} //finally //{ // if (stuEntity2 != null && stuEntity2.Connection.State != ConnectionState.Closed) // stuEntity2.Connection.Close(); //} #endregion Console.WriteLine("OK"); Console.Read(); } } }