//新增UserInfo userInfo = new UserInfo();userInfo.UserName = "YANG";userInfo.UserPass = "123";userInfo.Email = "253@qq.com";userInfo.RegTime = System.DateTime.Now;Model1Container db = new Model1Container();db.UserInfoSet.Add(userInfo);db.SaveChanges();Response.Write(userInfo.Id); //删除UserInfo userInfo = new UserInfo() { Id = 2 };Model1Container db =new Model1Container();// db.UserInfoSet.Remove(userInfo);//必须提前知道对象,不然报错,找不到对象db.Entry<UserInfo>(userInfo).State = System.Data.Entity.EntityState.Deleted;db.SaveChanges();//查询Model1Container db = new Model1Container();var userInfoList = from u in db.UserInfoSetwhere u.Id == 2select u;foreach (var userInfor in userInfoList){Response.Write(userInfor.UserName);}//修改Model1Container db = new Model1Container();var userInfoList = from u in db.UserInfoSetwhere u.Id == 3select u;var userInfo = userInfoList.FirstOrDefault();userInfo.UserName = "成龙";db.Entry<UserInfo>(userInfo).State = System.Data.Entity.EntityState.Modified;db.SaveChanges(); //一对多 新增 Model2Container db = new Model2Container();Customer customer = new Customer() { CustomerName = "张三", CustomerPwd = "123", SubTime = DateTime.Now };OrderInfo orderInfo1 = new OrderInfo() { id = Guid.NewGuid(), OrderNum = "10001", CreateDateTime = DateTime.Now, Customer = customer };OrderInfo orderInfo2 = new OrderInfo() { id = Guid.NewGuid(), OrderNum = "10002", CreateDateTime = DateTime.Now, Customer = customer };OrderInfo orderInfo3 = new OrderInfo() { id = Guid.NewGuid(), OrderNum = "10003", CreateDateTime = DateTime.Now, Customer = customer };db.CustomerSet.Add(customer);db.OrderInfoSet.Add(orderInfo1);db.OrderInfoSet.Add(orderInfo2);db.OrderInfoSet.Add(orderInfo3);db.SaveChanges(); .// 查询部分列Model1Container db =new Model1Container();var userInfoList = from u in db.UserInfoSetwhere u.Id == 3select new { userName = u.UserName, userPwd = u.UserPass };foreach (var userInfo in userInfoList){Response.Write(userInfo.userName + ":" + userInfo.userPwd);}//lamda表达式Model1Container db = new Model1Container();var userInfoList = db.UserInfoSet.Where<UserInfo>(u => u.Id > 4).OrderBy<UserInfo, int>(u=>u.Id);foreach (var userInfo in userInfoList){Response.Write(userInfo.UserName + ":" + userInfo.UserPass);}// 分页//Model1Container db = new Model1Container();//为了解决访问过程中出现多个EF连接,利用HttpContext对象的线程唯一对象属性,判断db访问对象是否存在 Model1Container db = null;if (HttpContext.Current.Items["db"] == null){db = new Model1Container();HttpContext.Current.Items["db"] = db;}else{db = HttpContext.Current.Items["db"] as Model1Container;}int pageSize = 2;int pageIndex = 2;var userInfoList = db.UserInfoSet.Where<UserInfo>(u => true).OrderByDescending<UserInfo, int>(u => u.Id).Skip<UserInfo>((pageIndex-1)*pageSize).Take<UserInfo>(pageSize); //如果转换为List,就无延迟加载 foreach (var userInfo in userInfoList){Response.Write(userInfo.UserName + ":" + userInfo.UserPass+"<br/>");} var userInfoList = db.UserInfoSet.Where<UserInfo>(u => u.Id > 0);//有延迟加载var userInfoList1 = db.UserInfoSet.Where<UserInfo>(u => u.Id > 0).ToList();//无延迟加载,直接将数据加载到内存中