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 = "",
age =
};
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)
{
//获取id
employee emp = new employee
{
id =
};
//获取数据库上下文对象
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 = ,
name = "例子",
passwd = "",
age =
};
//获取数据库上下文对象
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 > );
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().Take();
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 > );
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 > 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.employee
join i in dbContext.employeeInfo
on e.id equals i.emp_id
where e.id >
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(),则当数据遍历的时候请求。
Entity FrameWork 操作使用详情的更多相关文章
- Linq实战 之 Linq to Sql及Entity Framework操作详解
Linq实战 之 Linq to Sql及Entity Framework操作详解 一:linq to db的框架 1. linq to sql 2. linq to ado.net entity f ...
- EF ( Entity Framework) 操作ArcCataLog 生成的(Sql Server)空间数据库
因为项目需求,现在需要利用EF 操作由Arccatalog生成的sql server空间数据库..在此之前,一直没有接触过空间数据库,在操作空间数据库时 绕了许多弯... 因此写一篇随笔做一个总结. ...
- Entity FrameWork操作数据库完成登陆、列表显示+验证码
登陆页面 登陆页面的页面结构比较简单,没有写样式. image标签的作用是用来显示验证码. 一般处理程序代码展示 using System; using System.Collections.Gene ...
- Entity Framework 全面教程详解(转)
目录 预备知识 2 LINQ技术 2 LINQ技术的基础 - C#3.0 2 自动属性 2 隐式类型 2 对象初始化器与集合初始化器 3 匿名类 3 扩展方法 ...
- 转载Entity Framework全面教程
转载原地址:http://www.cnblogs.com/lsxqw2004/archive/2009/05/31/1495240.html#_Toc228672754 预备知识 2 LINQ技 ...
- Entity Framework 教程(转)
预备知识 2 LINQ技术 2 LINQ技术的基础 - C#3.0 2 自动属性 2 隐式类型 2 对象初始化器与集合初始化器 3 匿名类 3 扩展方法 ...
- [hystar整理]Entity Framework 教程
参考页面: http://www.yuanjiaocheng.net/entity/entity-relations.html http://www.yuanjiaocheng.net/entity/ ...
- .NET Core开发日志——Entity Framework与PostgreSQL
Entity Framework在.NET Core中被命名为Entity Framework Core.虽然一般会用于对SQL Server数据库进行数据操作,但其实它还支持其它数据库,这里就以Po ...
- Entity Framework的原理及使用方式
ADO.NET Entity Framework操作数据库的过程对用户是透明的(当然我们可以通过一些工具或方法了解发送到数据库的SQL语句等).我们唯一能做的是操作EDM,EDM会将这个操作请求发往数 ...
随机推荐
- noip模拟赛 解谜游戏
题目描述LYK进了一家古董店,它很想买其中的一幅画.但它带的钱不够买这幅画.幸运的是,老板正在研究一个问题,他表示如果LYK能帮他解出这个问题的话,就把这幅画送给它.老板有一个n*m的矩阵,他想找一个 ...
- angular的又一本好书
MANNING出的<ANGULAR.JS IN ACTION>. 比上本看完的书<ANGULAR ESSENTIAL>多了一些有全局性的东东. 八个关键概念:MODULE,CO ...
- 洛谷 P2195 HXY造公园
P2195 HXY造公园 题目描述 现在有一个现成的公园,有n个休息点和m条双向边连接两个休息点.众所周知,HXY是一个SXBK的强迫症患者,所以她打算施展魔法来改造公园并即时了解改造情况.她可以进行 ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- C语言里全局变量管理
C语言里信息封装比較弱,仅仅有静态变量的文件作用域. 假设不加约束.非常easy造成全局变量满天飞. 假设定义一个全局结构体.把全局变量都放到这个GlobleVariate里,应该好管一些,至少比裸奔 ...
- Codeforces Round #276 (Div. 1) A. Bits 贪心
A. Bits Let's denote as the number of bits set ('1' bits) in the binary representation of the non ...
- luogu3469 [POI2008]BLO_Blockade
题目大意 给一个无向连通图,求对于每一个点,去掉该点后图中连通结点有序对的减少量. 思路 当时想这道题时,我想到:枚举每一个点,在删去它后连通的几个部分中Dfs得到各个部分的点的个数从而得到解,但是我 ...
- Java 8 实战 P2 Functional-style data processing
目录 Chapter 4. Introducing streams Chapter 5. Working with streams Chapter 6. Collecting data with st ...
- Python入门 六、像个 Pythonista
pickle import pickle test_data = ['Save me!',123.456,True] f = file('test.data','w') pickle.dump(tes ...
- Genesis 多边形闭轮廓填充算法
通过逐行扫描,计算得出直线与多边形相交点进行求解 原理图形如下所示: 相关函数: /// <summary> /// 求点P到线段L距离 /// </summary> /// ...