using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int result = 0; using (Entities en = new Entities()) { Console.WriteLine("1.add test"); z z1=new z(){name="testName"}; en.AddToz(z1); result=en.SaveChanges(); Console.WriteLine(result>0?"add success":"add fail"); Console.WriteLine("2.del test"); //1.只能用FirstOrDefault不能用First; z z2 = en.z.FirstOrDefault(p => p.id == 105); //2.必须判断是否为null if(z2!=null) en.DeleteObject(z2); result = en.SaveChanges(); Console.WriteLine(result > 0 ? "del success" : "del fail"); Console.WriteLine("3.update test"); z1 = en.z.First(p=>p.id==107); z1.name = "updateTest"; result = en.SaveChanges(); Console.WriteLine(result > 0 ? "update success" : "update fail"); Console.WriteLine("4.query test"); List<z> list= en.z.ToList(); foreach(z zo in list){ Console.WriteLine("id:"+zo.id+",name:"+zo.name); } Console.WriteLine("4.2 pagination query "); int pageIndex = 2; //当前页面数(注:页码从1开始) int pageSize = 2; //每页的记录数 int skipCount = (pageIndex - 1) * pageSize; //需要跳过的记录数 List<z> pList = en.z .OrderBy(zz => zz.id)//.OrderByDescending则为降序 .Skip(skipCount) //跳过记录数 .Take(pageSize) //取的记录数 .ToList(); //形成泛型集合 foreach (z zo in pList) { Console.WriteLine("id:" + zo.id + ",name:" + zo.name); } Console.ReadLine(); } } } }