第四篇 Entity Framework Plus 之 Batch Operations
用 Entity Framework 进行 增,删,改。都是基于Model进行的,且Model都是有状态追踪的。这样Entity Framework才能正常增,删,改。
有时候,要根据某个字段,批量更新或者删除数据,用Entity Framework就会显得很是繁琐,且不高效。
Entity Framework Plus 为Entity Framework 提供 BatchUpdate 和 BatchDelete 操作扩展。使得更新和删除数据,变得简单而高效了许多。
废话不多说,直接实践给大家看。
一. 创建项目以及相关代码展示,还是之前的解决方案 “EntityFrameworkPlusSolution”。
1. 在解决方案,新增”EntityFrameworkPlus.BatchOperations.Demo“ WinForm 项目。

在项目中分别新增 “BatchOperations”,“BatchUpdate”,“BatchDelete” 窗口,每个窗口布局和代码如下。
BatchOperations (BatchUpdate,BatchDelete 窗口的入口)

using System;
using System.Windows.Forms; namespace EntityFrameworkPlus.BatchOperations.Demo
{
public partial class BatchOperations : Form
{
public BatchOperations()
{
InitializeComponent();
} private void btnBatchUpdate_Click(object sender, EventArgs e)
{
new BatchUpdate().ShowDialog();
} private void btnBatchDelete_Click(object sender, EventArgs e)
{
new BatchDelete().ShowDialog();
}
}
}
BatchUpdate

using System;
using System.Linq;
using System.Windows.Forms;
using EntityFrameworkPlus.DbContext;
using EntityFrameworkPlus.Models;
using Z.EntityFramework.Plus; namespace EntityFrameworkPlus.BatchOperations.Demo
{
public partial class BatchUpdate : Form
{
public BatchUpdate()
{
InitializeComponent();
SearchGood();
} public void SearchGood()
{
using (var db = new EntityFrameworkPlusDbContext())
{
dgvList.DataSource = db.Goodses.ToList();
}
} private void btnUpdateWithSearch_Click(object sender, EventArgs e)
{
var creator = txtCreator.Text.Trim();
var unitPrice = Convert.ToDecimal(txtUnitPrice.Text.Trim()) ;
using (var db = new EntityFrameworkPlusDbContext())
{
db.Goodses.Where(c => c.Creator.Equals(creator)).Update(c => new GoodsModel {UnitPrice = unitPrice});
}
SearchGood();
}
}
}
BatchDelete

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Forms;
using EntityFrameworkPlus.DbContext;
using EntityFrameworkPlus.Models;
using Z.EntityFramework.Plus; namespace EntityFrameworkPlus.BatchOperations.Demo
{
public partial class BatchDelete : Form
{
public BatchDelete()
{
InitializeComponent();
SearchGood();
}
public void SearchGood()
{
using (var db = new EntityFrameworkPlusDbContext())
{
dgvList.DataSource = db.Goodses.ToList();
}
}
private void btnDeleteWithSearch_Click(object sender, EventArgs e)
{
using (var db = new EntityFrameworkPlusDbContext())
{
var unitPrice = Convert.ToDecimal(txtUnitPrice.Text);
// ReSharper disable once NotAccessedVariable
Expression<Func<GoodsModel, bool>> whereExpression = null;
if (cbxOperation.Text.Equals("="))
{
whereExpression = d => d.UnitPrice == unitPrice;
}
if (cbxOperation.Text.Equals(">="))
{
whereExpression = d => d.UnitPrice >= unitPrice;
}
if (cbxOperation.Text.Equals(">"))
{
whereExpression = d => d.UnitPrice > unitPrice;
}
if (cbxOperation.Text.Equals("<="))
{
whereExpression = d => d.UnitPrice <= unitPrice;
}
if (cbxOperation.Text.Equals("<"))
{
whereExpression = d => d.UnitPrice < unitPrice;
} db.Goodses.Where(whereExpression).Delete();
}
SearchGood();
} }
}
2. Demo 数据,还是拿商品数据。
BatchUpdate Demo的是 根据Creator,更新单价,SQL表示大概 update Sample_Goods set UnitPrice = 100 where Creator = 'david' 。
BatchDelete 根据UnitPrice = ,< , > 来删除商品,SQL 表示大概 delete Sample_Goods where UnitPrice(=|>|<)100
二 .测试结果
1. BatchUpdate
1>.初始化窗口

2.>执行之前

3.> 执行之后

2. BatchDelete
1.>初始化窗口

2.>执行之前

3.>执行之后

这篇又到这里了,该结束了,Entity Framework Plus 系统四篇博文,已经全部结束了,从之前博文评论来说,有人觉得 Entity Framework Plus 是侵入的,这里我要说明一下,大家不要被我糟糕的Demo,没有一点封装所引导,我这里只是简单的介绍,作为一个引子,供大家学习,Entity Framework Plus 是一个扩展工具,需要大家封装一下。比喻引用在DDD里面。
源代码:https://github.com/haibozhou1011/EntityFramework-PlusSample
第四篇 Entity Framework Plus 之 Batch Operations的更多相关文章
- 第三篇 Entity Framework Plus 之 Query Cache
离上一篇博客,快一周,工作太忙,只能利用休息日来写一些跟大家分享,Entity Framework Plus 组件系列文章,之前已经写过两篇 第一篇 Entity Framework Plus 之 A ...
- 第二篇 Entity Framework Plus 之 Query Future
从性能的角度出发,能够减少 增,删,改,查,跟数据库打交道次数,肯定是对性能会有所提升的(这里单纯是数据库部分). 今天主要怎样减少Entity Framework查询跟数据库打交道的次数,来提高查询 ...
- 第一篇 Entity Framework Plus 之 Audit
一般系统会有登陆日志,操作日志,异常日志,已经满足大部分的需求了.但是有时候,还是需要Audit 审计日志,审计日志,主要针对数据增,改,删操作数据变化的记录,主要是对数据变化的一个追踪过程.其中主要 ...
- .NET基础篇——Entity Framework 数据转换层通用类
在实现基础的三层开发的时候,大家时常会在数据层对每个实体进行CRUD的操作,其中存在相当多的重复代码.为了减少重复代码的出现,通常都会定义一个共用类,实现相似的操作,下面为大家介绍一下Entity F ...
- Entity Framework Plus 系列目录
Entity Framework Plus 系列文章计划的已经全部写完,可能还有其他功能没有写到,希望大家能够多动手,尝试一下使用,一定会给您带来一些帮助的.文章全部写完,也应该出一个目录方便查看,目 ...
- entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等
前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和 ...
- C# Entity Framework并发处理
原网站:C# Entity Framework并发处理 在软件开发过程中,并发控制是确保及时纠正由并发操作导致的错误的一种机制.从 ADO.NET 到 LINQ to SQL 再到如今的 ADO.NE ...
- C#综合揭秘——Entity Framework 并发处理详解
引言 在软件开发过程中,并发控制是确保及时纠正由并发操作导致的错误的一种机制.从 ADO.NET 到 LINQ to SQL 再到如今的 ADO.NET Entity Framework,.NET 都 ...
- Entity Framework 6.1-Code First【转】
Entity Framework 6.1-Code First 分类: Entity Framework 2014-04-21 14:56 2034人阅读 评论(0) 收藏 举报 entityen ...
随机推荐
- vue入门学习(基础篇)
vue入门学习总结: vue的一个组件包括三部分:template.style.script. vue的数据在data中定义使用. 数据渲染指令:v-text.v-html.{{}}. 隐藏未编译的标 ...
- PHP类和对象之重载
PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的.属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值.读取.判断属性是否设置.销毁属性. ...
- 微信小程序前端源码逻辑和工作流
看完微信小程序的前端代码真的让我热血沸腾啊,代码逻辑和设计一目了然,没有多余的东西,真的是大道至简. 废话不多说,直接分析前端代码.个人观点,难免有疏漏,仅供参考. 文件基本结构: 先看入口app.j ...
- 微软发布VSBT,无需安装Visual Studio即可实现项目编译
安装了Visual Studio的那些使用微软平台的开发者通常能够非常容易地操作自己的项目:打开解决方案,修改内容,设置好所有必须的文件以及配置后编译项目.但是在构建服务器或者持续交付系统等没有安装V ...
- atitit.管理学三大定律:彼得原理、墨菲定律、帕金森定律
atitit.管理学三大定律:彼得原理.墨菲定律.帕金森定律 彼得原理(The Peter Principle) 1 彼得原理解决方案1 帕金森定律 2 如何理解墨菲定律2 彼得原理(The Pete ...
- Linux学习
Linux 命令英文全称su:Swith user 切换用户,切换到root用户cat: Concatenate 串联uname: Unix name 系统名称df: Disk free 空余硬盘du ...
- 简约而不简单的Django新手图文教程
本文面向:有python基础,刚接触web框架的初学者. 环境:windows7 python3.5.1 pycharm专业版 Django 1.10版 pip3 一.Django简介 百度百 ...
- 2DToolkit官方文档中文版打地鼠教程(一):初始设置
这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...
- 让ASP.NET5在Jexus上飞呀飞
就在最近一段时间,“Visual Studio 2015 CTP 5”(以下简称CTP5)发布了,CTP5的发布不仅标志着新一代的VisualStudio正式发布又向前迈出了一步,还标志着距离ASP. ...
- 微软发布 Windows Server 2016 预览版第三版,开发者要重点关注Nano Server
微软已经发布 Windows Server 2016 和 System Center 2016 第三个技术预览版,已经提供下载.Windows Server 2016 技术预览版第三版也是首个包括了容 ...