StudentEntities entities = new StudentEntities(); //初始化对象 Register register = new Register(); #region 增加 //register.UserName = "张三"; //register.Pwd = "123"; //register.Sex = "男"; //register.Phone = "123456"; //register.Age = "20"; //register.Address = "河南郑州"; //entities.Register.Add(register); //添加一个集合使用Add //entities.SaveChanges(); //保存 #endregion #region 删除 //Register r = entities.Register.Where(i=>i.Id==39).SingleOrDefault(); //在这里需要注意的是Lambda表达式的i属性Register,它是一个集合,所以需要它.出来他的id项 //entities.Register.Remove(r); //删除使用的是Remove //entities.SaveChanges(); #endregion #region 修改 //Register rr = entities.Register.Where(i => i.Id == 40).SingleOrDefault(); //修改时间记得先查询要修改的条件 //rr.UserName = "zhangsan"; //在这里需要注意的是接受的对象 //rr.Pwd = "456"; //rr.Sex = "女"; //rr.Phone = "123456789"; //rr.Age = "20"; //rr.Address = "北京"; //entities.SaveChanges(); //保存 #endregion #region 查询 //Register rrr = entities.Register.Where(i => i.Id == 41).SingleOrDefault(); //Console.WriteLine("{0},{1},{2}",rrr.Id,rrr.UserName,rrr.Pwd); //Register rrr = entities.Register.SelectMany(i=>i.Id==40&&i.Id==41).SelectMany();
原文:http://www.cnblogs.com/laopo/p/5133475.html