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.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();//无延迟加载,直接将数据加载到内存中

 

转载于:https://www.cnblogs.com/CelonY/p/10650016.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279757.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何使用echo.js实现图片的懒加载(整理)

如何使用echo.js实现图片的懒加载&#xff08;整理&#xff09; 一、总结 一句话总结&#xff1a;a、在img标签中添加data-echo属性加载真实图片&#xff1a;<img class"loading" src"blank.gif" data-echo"图片真实路径" />&#xff0c;在…

还原出厂设置 擦除frp_如何备份,擦除和还原Apple Watch

还原出厂设置 擦除frpThe Apple Watch is, in its own right, a little tiny computer with data backup and security needs. Read on as we show you how to ensure your Apple Watch is backed up, wiped, and restored just like you’d do with your smartphone. Apple Wa…

exchange 2010 search mailbox 的幕后强大功能

铃……….半夜中被一阵急促的手机铃声吵醒&#xff0c;年度服务客户打来电话需要进行邮件的排查和删除工作。问其原因&#xff0c;原来是组织中有人发了一封关于领导的不健康的邮件&#xff0c;并在企业内部进行了转发&#xff0c;领导要求立即找出此类邮件并进行删除。管理员深…

Apache HBase的现状和发展

一、&#xff28;Base是什么 HBase(Hadoop Database)&#xff0c;是一个基于Google BigTable论文设计的高可靠性、高性能、可伸缩的分布式存储系统。 它有以下特征&#xff1a; 1.HBase仍然是采用行存储的&#xff0c;采用松散表的结构来获得动态列的功能&#xff1b; 2.原生海…

Java的接口、继承与多态

接口 java只支持单继承&#xff0c;即一个类只能有一个父类&#xff0c;因此需要接口来实现多重继承。 接口的定义 类和接口的区别&#xff1a;一个类通过继承接口的方式&#xff0c;从而来继承接口的抽象方法。类描述对象的属性和方法&#xff0c;接口则包含类要实现的方法。 …

dvd刻录软件_如何在Windows 7中刻录照片和视频DVD(无需额外的软件)

dvd刻录软件Software like DVD Flick is great for burning video to DVDs, but Windows 7 actually includes built-in DVD burning software. Strangely, it’s the last time the company did so—while Windows 8 and Windows 10 can play back DVD movies, they can’t cr…

如何实现office不同语言界面切换

前面我讨论了《如何实现win7不同语言界面切换》&#xff0c;很巧今天又有网友问到如何实现 office的语言界面切换呢。嘿&#xff0c;那我们就继续来玩转界面吧。 office2007和office2010也支持轻松的进行语言界面切换&#xff0c;操作步骤也很简单。 Office 语言界面包 (LIP) 是…

Mysql-高可用集群[MyCat中间件使用](三)

服务器-节点: 4台 mysql-主: 192.168.2.40mysql-从-node-0: 192.168.2.41mysql-从-node-1: 192.168.2.42mycat: 192.168.2.45操作过程 1.搭建mysql主从节点2.搭建mycat中间件节点3.mycat服务配置4.测试读写分离,读的分发1.搭建mysql主从节点 Mysql-高可用集群主从单一模式-binl…

yum安装mysql5.6

1.检查系统是否安装其他版本的MYSQL数据 yum list installed | grep mysql yum -y remove mysql-libs.x86_64 2.安装及配置 wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm rpm -ivh mysql-community-release-el6-5.noarch.rpm yum repolist all | grep…

离开互联网行业_如何使用互联网再也不会离开家

离开互联网行业Thanks to the Internet, activities like “going outside” or “being a productive member of the community” are becoming increasingly optional parts of daily life. When your inner hermit feels like putting on his vampire cape, simple tricks l…

iOS 11开发教程(十三)iOS11应用编辑界面添加视图

iOS 11开发教程&#xff08;十三&#xff09;iOS11应用编辑界面添加视图 在iOS中添加视图的方式有两种&#xff1a;一种是使用编辑界面添加视图&#xff1b;另一种是使用代码添加视图。以下是这两个方式的详细介绍。 1.编辑界面添加视图 使用编辑界面添加视图是一个相当简单的工…

限流算法(记录cyc大佬的专栏)

限流的必要性 如果一段时间内请求的数量过大&#xff0c;就会给服务器造成很大压力&#xff0c;可能导致服务器无法提供其它服务。 计数器算法 通过一个计数器 counter 来统计一段时间内请求的数量&#xff0c;并且在指定的时间之后重置计数器。该方法实现简单&#xff0c;但是…

bzoj 1024 [ SCOI 2009 ] 生日快乐 —— 递归

题目&#xff1a;https://www.lydsy.com/JudgeOnline/problem.php?id1024 因为每次把一块切成两块&#xff0c;所以可以枚举从哪里切开&#xff0c;然后递归求解&#xff1b; 一开始用了不太对的贪心思路&#xff0c;想着一定去切较长边&#xff0c;但看来不一定。 代码如下&a…

HBase存储剖析与数据迁移

1.概述 HBase的存储结构和关系型数据库不一样&#xff0c;HBase面向半结构化数据进行存储。所以&#xff0c;对于结构化的SQL语言查询&#xff0c;HBase自身并没有接口支持。在大数据应用中&#xff0c;虽然也有SQL查询引擎可以查询HBase&#xff0c;比如Phoenix、Drill这类。但…

windows os x_如何立即在OS X上获取Windows样式的窗口捕捉

windows os xApple’s recent announcement that the upcoming OS X release (El Capitan or 10.11) will finally, at long last, come with the ability to snap windows to your screen edges. A feature Windows users have enjoyed since 2009. 苹果公司最近宣布即将发布的…

Install Odoo 11 on CentOS 7

2019独角兽企业重金招聘Python工程师标准>>> Odoo is the most popular all-in-one business software in the world. It offers a range of business applications including CRM, website, e-Commerce, billing, accounting, manufacturing, warehouse, project m…

创建maven项目,配置maven地址

2019独角兽企业重金招聘Python工程师标准>>> 在eclipse中&#xff0c;新建maven项目next第二步&#xff0c;选择webapp输入id及包名&#xff0c;完成新建给项目build path&#xff0c;添加1.8版本jdk和tomcat项目右键属性 project Facets&#xff0c;切换成web项目&…

如何在Excel中隐藏单元格,行和列

There may be times when you want to hide information in certain cells or hide entire rows or columns in an Excel worksheet. Maybe you have some extra data you reference in other cells that does not need to be visible. 有时您可能想在某些单元格中隐藏信息或在…

金三银四,跳槽为敬

不是不想跳&#xff0c;是如今的市场水冷&#xff0c;挪不开脚。 三月有“黄金”。 过去很多年&#xff0c;这个被誉为市场黄金期的阶段&#xff0c;最热闹的旗帜属于房地产&#xff0c;其次是人才市场。跳槽二字&#xff0c;充满诱惑。对每一个想要改变的人而言&#xff0c;…

yum搭建本地仓库、国内源、下载rpm包、源码安装

yum搭建本地仓库[rootlocalhost ~]# mount /dev/cdrom /mnt/mount: /dev/sr0 写保护&#xff0c;将以只读方式挂载cp -r /etc/yum.repos.d /etc/yum.repos.d.bak[rootlocalhost ~]# cp -r /etc/yum.repos.d/ /etc/yum.repos.d.ori/ //复制rm -f /etc/yum.repos.d/*[rootlocalh…