第四篇 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 ...
随机推荐
- [原]Paste.deploy 与 WSGI, keystone 小记
Paste.deploy 与 WSGI, keystone 小记 名词解释: Paste.deploy 是一个WSGI工具包,用于更方便的管理WSGI应用, 可以通过配置文件,将WSGI应用加载起来. ...
- .NET应用程序域
在.NET平台下,可执行程序并没有直接承载在Windows进程中,而非托管程序是直接承载的..NET可执行程序承载在进程的一个逻辑分区中,称之为应用程序域(AppDomain).一个进程可以包含多个应 ...
- autocomplete的使用
autocomplete使用分为本地调用方法和读取远程读取数据源的方法 (1)本地调用方法 <script src="Scripts/jquery-1.4.1.min.js" ...
- css3制作旋转动画
现在的css3真是强大,之前很多动画都是用jq来实现,但是css3制作的动画要比jq实现起来简单很多,今天呢,我自己也写了一个css旋转动画和大家分享.效果如下面的图片 思路:1.制作之前呢,我们先来 ...
- web 前端(轮番插件)
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...
- 如何区别exists与not exists?
1.exists:sql返回结果集为真:not exists:sql不返回结果集为真.详解过程如图: exists not exists
- ABP源码分析三十二:ABP.SignalR
Realtime Realtime是ABP底层模块提供的功能,用于管理在线用户.它是使用SignalR实现给在线用户发送通知的功能的前提 IOnlineClient/OnlineClient: 封装在 ...
- JDBC基础
今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...
- SVN源代码的版本控制系统使用简介
SVN是以个开放源代码的版本控制系统,当前最流行的版本控制系统,GIT是近段时间刚兴起的. 下面开始介绍如何安装也配置 1先下载或者从别的地方弄一个安装包(本人是64位的,32位的就用32位的安装包) ...
- iOS App引导页功能实现
一.写作原因 以前都没有想着来写点东西,今天遇到件事情让我决定每次还是要做记录.因为以前自己可以轻松的完成pod spec的配置,但是今天在做的时候还是忘了遇到了很多坑.pod spec配置遇到的坑不 ...