• 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();//无延迟加载,直接将数据加载到内存中
                
  • 相关阅读:
    webpack搭建vue项目,实现脚手架功能
    【学习笔记】JS设计模式总结
    【学习笔记】JS经典异步操作,从闭包到async/await
    【学习笔记】深入理解async/await
    【学习笔记】剖析MVVM框架,简单实现Vue数据双向绑定
    【学习笔记】node.js重构路由功能
    使用react-app-rewired和customize-cra 个性化配置
    axios 生产环境和开发环境 ip 切换(修改文件配置)
    socketio 更改requsturl 实现nginx 代理多个服务端
    vue-json-editor 简单实现
  • 原文地址:https://www.cnblogs.com/CelonY/p/10650016.html
Copyright © 2020-2023  润新知