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 操作使用详情的更多相关文章

  1. 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 ...

  2. EF ( Entity Framework) 操作ArcCataLog 生成的(Sql Server)空间数据库

    因为项目需求,现在需要利用EF 操作由Arccatalog生成的sql server空间数据库..在此之前,一直没有接触过空间数据库,在操作空间数据库时 绕了许多弯... 因此写一篇随笔做一个总结. ...

  3. Entity FrameWork操作数据库完成登陆、列表显示+验证码

    登陆页面 登陆页面的页面结构比较简单,没有写样式. image标签的作用是用来显示验证码. 一般处理程序代码展示 using System; using System.Collections.Gene ...

  4. Entity Framework 全面教程详解(转)

    目录 预备知识    2 LINQ技术 2 LINQ技术的基础 - C#3.0    2 自动属性    2 隐式类型    2 对象初始化器与集合初始化器    3 匿名类    3 扩展方法    ...

  5. 转载Entity Framework全面教程

    转载原地址:http://www.cnblogs.com/lsxqw2004/archive/2009/05/31/1495240.html#_Toc228672754 预备知识    2 LINQ技 ...

  6. Entity Framework 教程(转)

    预备知识    2 LINQ技术    2 LINQ技术的基础 - C#3.0    2 自动属性    2 隐式类型    2 对象初始化器与集合初始化器    3 匿名类    3 扩展方法    ...

  7. [hystar整理]Entity Framework 教程

    参考页面: http://www.yuanjiaocheng.net/entity/entity-relations.html http://www.yuanjiaocheng.net/entity/ ...

  8. .NET Core开发日志——Entity Framework与PostgreSQL

    Entity Framework在.NET Core中被命名为Entity Framework Core.虽然一般会用于对SQL Server数据库进行数据操作,但其实它还支持其它数据库,这里就以Po ...

  9. Entity Framework的原理及使用方式

    ADO.NET Entity Framework操作数据库的过程对用户是透明的(当然我们可以通过一些工具或方法了解发送到数据库的SQL语句等).我们唯一能做的是操作EDM,EDM会将这个操作请求发往数 ...

随机推荐

  1. 清北学堂模拟赛d4t2 b

    分析:比较复杂的一题. 首先要求k个mod m互不相同且和为n的数ai,我们可以转化为求和为k个bi,并且(Σbi) % m = n % m 其中bi=ai % m,接下来可以用dp求出选了i个b,和 ...

  2. [TS-A1487][2013中国国家集训队第二次作业]分配游戏[二分]

    根据题意,设$3n$次比较中胜了$w$次,负了$l$次,平了$d$次,所有场次中胜了$W$次,负了$L$次,平了$D$次.如果一场赢了,那么$w-l$就会$+1$,相同地,$W-L$也会$+1$:如果 ...

  3. CODEVS3147 矩阵乘法2

    ...怎么优化都是90分,最后一个点一直T掉,有谁过了请告诉我. Program CODEVS3147; ; ..maxn,-..maxn] of longint; n,q,i,j,k,k1,k2,k ...

  4. 用c实现shell排序

    shell排序的方法又称缩小增量法,是对直接插入排序法的改进.至于对于分组后采用哪种排序方法实现,本例采用直接选择排序和直接插入排序,理论上讲,通过分组排序后,数据基本上有序,这时通过直接插入排序会比 ...

  5. static_cast、dynamic_cast reinterpret_cast

    关于强制类型转换的问题,很多书都讨论过,写的最详细的是C++ 之父的<C++ 的设计和演化>.最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_ ...

  6. 中缀表达式转逆波兰式(后缀表达式)求值 C++ Stack

    给一个包含小数的中缀表达式 求出它的值 首先转换为后缀表达式然后利用stack求出值 转换规则: 如果字符为'('  push else if 字符为 ')' 出栈运算符直到遇到‘(' else if ...

  7. ORACLE11G设置IP訪问限制

    出于数据安全考虑,对oracle数据库的IP做一些限制,仅仅有固定的IP才干訪问. 改动$JAVA_HOME/NETWORK/ADMIN/sqlnet.ora文件 添加下面内容(红色表示凝视): #开 ...

  8. linux 启动两个tomcat

    按照下面的步骤操作即可部署成功:一些具体操作命令就不详细说了,直接说有用的:1.在 /usr/local 下部署两个Tomcat,tomcat的文件夹重命名为:tomcat6-1  .  tomcat ...

  9. jQuery总结04

    1 JavaScript 中的 AJAX 的四个实现步骤分别是? 2 如何处理 XMLHttpRequest 对象的兼容问题? 3 jQuery 中的 AJAX 4 jQuery 选择器包括哪些? 5 ...

  10. 20170322js面向对象

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...