热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

EntityFrameWork操作使用详情

EntityFrameWork是以ADO.net为基础发展的ORM解决方案。一、安装EntityFrameWork框架二、添加ADO.Net实体数据模型三、EF插入数据usingS

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


推荐阅读
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • EPICS Archiver Appliance存储waveform记录的尝试及资源需求分析
    本文介绍了EPICS Archiver Appliance存储waveform记录的尝试过程,并分析了其所需的资源容量。通过解决错误提示和调整内存大小,成功存储了波形数据。然后,讨论了储存环逐束团信号的意义,以及通过记录多圈的束团信号进行参数分析的可能性。波形数据的存储需求巨大,每天需要近250G,一年需要90T。然而,储存环逐束团信号具有重要意义,可以揭示出每个束团的纵向振荡频率和模式。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • VueCLI多页分目录打包的步骤记录
    本文介绍了使用VueCLI进行多页分目录打包的步骤,包括页面目录结构、安装依赖、获取Vue CLI需要的多页对象等内容。同时还提供了自定义不同模块页面标题的方法。 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
author-avatar
小遥2502881765
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有