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会将这个操作请求发往数 ...
随机推荐
- [luoguP1197] [JSOI2008]星球大战(并查集)
传送门 思维!重要的是思维! 题目让删边,然而并查集不好删边(并!查!集!啊) 我们离线处理,从后往前添边,这样并查集就可以用了. 用并查集维护连通块个数即可. ——代码 #include <c ...
- kendo grid create
这种自定义的create中的函数,这个data的行为是在发送到后端之前执行的 //{ // url: "/admgr/AdUserAuthorityAdd", // dataTyp ...
- codevs——T1214 线段覆盖
http://codevs.cn/problem/1214/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- Mac下使用OpenMP
Mac下使用OpenMP,修改Build Options 下面的compiler for c/c++/objective-C 为 LLVM GCC 4.2 - Language 则可以找到Enable ...
- javascript正則表達式
定义一个正則表達式 能够用字面量 var regex = /xyz/; var regex = /xyz/i; 也能够用构造函数 var regex = new RegExp('xyz'); var ...
- Python帮助函数调试函数 用于获取对象的属性及属性值
Python帮助函数调试函数 用于获取对象的属性及属性值 刚接触Python,上篇 <Python入门>第一个Python Web程序--简单的Web服务器 中调试非常不方便,不知道对象详 ...
- samba笔记
############ 1.安装网络yum ############ 2.安装createrepo [root@localhost ~]# yum install createrepo-0.9.8- ...
- QFileDialog关于选择文件对话框中的几个信号的说明(currentChanged,directoryEntered,fileSelected,filterSelected)
QFileDialog关于选择文件对话框中的几个信号 实例: openFile::openFile(QWidget *parent) :QWidget(parent),ui(new Ui::openF ...
- 【POJ 3630】 Phone List
[题目链接] http://poj.org/problem?id=3630 [算法] 字典树 [代码] #include <algorithm> #include <bitset&g ...
- TCP打开文件传输(客户端code)
#include <stdio.h>#include <stdlib.h>#include <arpa/inet.h>#include <sys/types. ...