zoukankan      html  css  js  c++  java
  • EF ORM

                 //新增
                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.UserInfoSet
                                   where u.Id == 2
                                   select u;
                foreach (var userInfor in userInfoList)
                {
                    Response.Write(userInfor.UserName);
                }
                //修改
                Model1Container db = new Model1Container();
                var userInfoList = from u in db.UserInfoSet
                                   where u.Id == 3
                                   select 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.UserInfoSet
                                   where u.Id == 3
                                   select 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();//无延迟加载,直接将数据加载到内存中
                
  • 相关阅读:
    模块安装
    yagmail 邮件模块
    unittest 单元测试
    用 yaml 写测试用例
    nnlog 日志模块
    Jenkins安装以及邮件配置
    数据驱动之 python + requests + Excel
    url 编码解码
    15. 测试环境部署-linux
    vue使用UEditor富文本编辑器
  • 原文地址:https://www.cnblogs.com/CelonY/p/10650016.html
Copyright © 2011-2022 走看看