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模拟赛 fateice-string
题目背景 Aldnoah ——火星上超古代文明留下的能量源,承认初代火星移民雷伊·雷加利亚博士(即后来的薇瑟帝国初代皇帝)为正统继承者,启动因子融入皇族的遗传因子中,只有皇族天生具有Aldnoah的启 ...
- IBM CEO罗睿兰:科技公司屹立百年的3个秘诀
假设有不论什么科技公司能够完美阐释"转型"这个词的含义,那么这家公司非创立103年的IBM莫属. 如今,它的变化更胜以往. 在<財富>杂志周二于美国加利福尼亚州拉古纳尼 ...
- Bitcask存储模型
----<大规模分布式存储系统:原理解析与架构实战>读书笔记 近期一直在分析OceanBase的源代码,恰巧碰到了OceanBase的核心开发人员的新作<大规模分布式存储系统:原理解 ...
- 大学,助你成长or 让你堕落?
不管是论坛.贴吧.还是博客,都或多或少能够看到诸如对大学教育的反思.抨击之类的文章.至于什么是大学,大学又该怎样度过.大学是助你成长还是让你堕落了?我想这应该是一个见仁见智的问题.作为一个过来人,结合 ...
- 开源 java CMS - FreeCMS2.2 工作流管理
项目地址:http://www.freeteam.cn/ 工作流管理 从FreeCMS 2.2開始支持 管理系统中使用到的工作流,如信息审核工作流. 1. 工作流组管理 从左側管理菜单点击工作流组管理 ...
- WCF学习笔记——不支持内容类型 text/xml; charset=utf-8
我在使用WCF的时候,客户端运行报错: 不支持内容类型 text/xml; charset=utf-8 原因是WCF服务做了修改.刷新客户端的服务引用,问题消失 =================== ...
- spring:按照Bean的名称自动装配User
本实例将介绍如何按照Bean 的名称自动装配 User 对象! <bean> 元素的 autowire 属性负责自动装配 <bean> 标签,定义 JavaBean 的属性.这 ...
- java-com-util-common-service:BaseService.java
ylbtech-java-com-util-common-service:BaseService.java 1.返回顶部 1. package com.shineyoo.manager.util.co ...
- 第13课 SmartGit程序操作介绍
http://www.syntevo.com/
- bzoj3436: 小K的农场(差分约束)
3436: 小K的农场 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1575 Solved: 690[Submit][Status][Discus ...