Entity FrameWork 操作使用详情

Entity FrameWork 是以ADO.net为基础发展的ORM解决方案。

一、安装Entity FrameWork框架

二、添加ADO.Net实体数据模型

三、EF插入数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){//获取数据库上下文对象testContext dbContext = new testContext();//创建数据实体employee emp = new employee{name = "yangs",passwd = "123",age = 18};dbContext.employee.Add(emp);//提交数据
            dbContext.SaveChanges();Console.WriteLine(emp.id);Console.ReadKey();}}
}

 

四、EF删除数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){//获取idemployee emp = new employee{id = 18};//获取数据库上下文对象testContext dbContext = new testContext();//将实体添加到数据库上下文
            dbContext.employee.Attach(emp);//对数据删除
            dbContext.employee.Remove(emp);//保存
            dbContext.SaveChanges();}}
}

 

五、EF修改数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){//准备实体employee emp = new employee{id = 11,name = "例子",passwd = "123",age = 44};//获取数据库上下文对象testContext dbContext = new testContext();//将实体添加到数据库上下文
            dbContext.employee.Attach(emp);dbContext.Entry(emp).State = System.Data.Entity.EntityState.Modified;dbContext.SaveChanges();}}
}

六、EF 查询数据

1.简单查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){testContext dbContext = new testContext();var list = dbContext.employee;foreach (var item in list){Console.WriteLine(item.name);}Console.ReadKey();}}
}

2.where 查询条件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){testContext dbContext = new testContext();var list = dbContext.employee.Where(p => p.id > 5);foreach (var item in list){Console.WriteLine(item.name);}Console.ReadKey();}}
}

2. skip(10) => 逃过10条数据,take(10) => 获取10条数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){testContext dbContext = new testContext();var list = dbContext.employee.Skip(1).Take(2);foreach (var item in list){Console.WriteLine(item.name);}Console.ReadKey();}}
}

 3. orderBy 排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){testContext dbContext = new testContext();//降序var list = dbContext.employee.OrderByDescending(p => p.id);foreach (var item in list){Console.WriteLine(item.name);}Console.ReadKey();}}
}

4. select 查询某几个字段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){testContext dbContext = new testContext();//降序var list = dbContext.employee.Select(p => new { p.id, p.name }).Where(p => p.id > 5);foreach (var item in list){Console.WriteLine(item.id+" -- "+item.name);}Console.ReadKey();}}
}

5. EF高级写法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){testContext dbContext = new testContext();//降序var list = from o in dbContext.employee where o.id > 5 select o; foreach (var item in list){Console.WriteLine(item.id+" -- "+item.name);}Console.ReadKey();}}
}

join 联合查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){testContext dbContext = new testContext();//降序var list = from e in dbContext.employeejoin i in dbContext.employeeInfoon e.id equals i.emp_idwhere e.id > 5 select new {e, i.content};}}
}

 七、延迟加载

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo
{class Program{static void Main(string[] args){testContext dbContext = new testContext();//降序var list = dbContext.employee.ToList();foreach (var item in list){}}}
}

toList()转换为本地内存数据级别,会请求数据。如果没有toList(),则当数据遍历的时候请求。

 

转载于:https://www.cnblogs.com/yang-2018/p/10222140.html

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

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

相关文章

[css] CSS选择器有哪些?哪些属性可以继承?

[css] CSS选择器有哪些?哪些属性可以继承? 选择器通配符idclass标签后代选择器子选择器兄弟选择器属性选择器伪类选择器伪元素选择器可以继承的属性font-sizefont-weightfont-stylefont-familycolor个人简介 我是歌谣,欢迎和大家一起交流前…

《redis 设计与实现》读书笔记

大家好,我是烤鸭: 《redis 设计与实现》,读书笔记。 第一部分 数据结构与对象 第2章 简单动态字符串 Redis 使用SDS 作为字符串表示。 O(1) 复杂度获取字符串长度。 杜绝缓冲区溢出。 减少修改字符串长度时所需的内存重分配次数。 …

网络通信中TCP出现的黏包以及解决方法 socket 模拟黏包

粘包问题概述 1.1 描述背景 采用TCP协议进行网络数据传送的软件设计中,普遍存在粘包问题。这主要是由于现代操作系统的网络传输机制所产生的。我们知道,网络通信采用的套接字(socket)技术,其实现实际是由系统内核提供一片连续缓存(流缓冲)来…

[css] 在页面上隐藏元素的方法有哪些?

[css] 在页面上隐藏元素的方法有哪些? position配合z-index; 或者 left/top/bottom/right : -100%;margin-left: -100%;width: 0; height: 0; overflow: hidden;这个算吗opacity: 0;display:none;transform: scale(0)/translateX(-100%)/tran…

[css] CSS3有哪些新增的特性?

[css] CSS3有哪些新增的特性? 边框圆角border-radius盒子阴影box-shadow文字阴影text-shadow2d、3d变换transformrotate()scale()skew()translate()过度动画transition自定义动画animation(只记得这些了)个人简介 我是歌谣,欢迎和大家一起交流前后端知…

windows docker redis 集群部署

大家好,我是烤鸭: 上次分享了windows docker redis,这么快就不够用了,单机的不行,整个集群的,看了网上的教程都好麻烦,简单点。 单机的:https://blog.csdn.net/Angry_Mills/article…

[css] 圣杯布局和双飞翼布局的理解和区别,并用代码实现

[css] 圣杯布局和双飞翼布局的理解和区别,并用代码实现 一: section{height: 100%; overflow: hidden;clear:both; } .left{ height: 100%;float:left;width:30%;background: #f00; } .right{ height: 100%;float:right;width:30%; background: #0f0; }…

某音数据分析

大家好,我是烤鸭: 某音竟然有pc版了,不过搜索的数据有限,会限制条数,亲测只能搜索400条数据,简单分析下过程。 工具使用 java chromedriver fiddler java selenium 自动化网页,需要登录&a…

Codeforces Round #530 Div. 1 自闭记

A&#xff1a;显然应该让未确定的大小尽量大。不知道写了啥就wa了一发。 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #…

自研redis sdk支持自动dns切换(附源码)

大家好&#xff0c;我是烤鸭&#xff1a; 标题起的有点大了&#xff0c;说是自研&#xff0c;其实就是个封装&#xff0c;不过倒是解决了dns切换的问题&#xff08;虽然不太优雅&#xff09;。 背景 之前做活动的时候&#xff0c;用域名链接的redis&#xff0c;当时做了主备集…

《黑客与画家》读书笔记

《黑客与画家》读书笔记 大家好&#xff0c;我是烤鸭&#xff1a; 《黑客与画家》&#xff0c;读书笔记。这次想修改以前那种章节式的笔记&#xff0c;一个是这本书是比较主观的&#xff0c;一个是想换个风格。 作者 保罗格雷厄姆&#xff08;Paul Graham&#xff09;&a…

使用Canal实现redis和mysql的同步

使用Canal实现redis和mysql的同步 ### canal 工作思路 Canal 会将自己伪装成 MySQL 从节点&#xff08;Slave&#xff09;&#xff0c;并从主节点&#xff08;Master&#xff09;获取 Binlog&#xff0c;解析和贮存后供下游消费端使用。Canal 包含两个组成部分&#xff1a;服务…

上线到凌晨4点半 pagehelper的bug?

大家好&#xff0c;我是烤鸭&#xff1a; 上上周末上线到凌晨4点半&#xff0c;哭了&#xff0c;没想到问题竟然如此简单。最近又懒惰了&#xff0c;写了开头就一直放着了&#xff0c;今天终于补上。 ​ 问题日志 Error querying database. Cause: com.github.pagehelper.P…

sql 查询结果自定义排序

sqlserver 使用case when then 语句来实现 select name from fruit order by case namewhen Strawberry then 1when Banana then 2when Apple then 3else 4 end oracle 使用decode实现 select * from table_example order by decode(class,C,1,A,2,D,3,B,4) 转载于:https://www…

skywalking 引起 spring-cloud-gateway 的内存溢出 skywalking的bug

大家好&#xff0c;我是烤鸭&#xff1a; 又是个线上问题记录&#xff0c;这次坑惨了&#xff0c;开源软件也不是万能的&#xff0c;还是要做好压测和灰度。 问题 上游反馈大量超时&#xff0c;不止某一个服务&#xff0c;查看服务没有问题&#xff0c;猜测是网络或者环境问题…

长连接检测 监控的一点思考 java实现

大家好&#xff0c;我是烤鸭&#xff1a; 怎么监控长链接服务器的稳定&#xff0c;除了探活服务之外&#xff0c;怎么保证长链接的收发正常&#xff0c;这篇文章考虑下这个。 问题来源 运营反馈部分直播间无法收到弹幕、点赞消息&#xff0c;第一时间进行复现&#xff0c;发现…