第四篇 Entity Framework Plus 之 Batch Operations

      用 Entity Framework  进行 增,删,改。都是基于Model进行的,且Model都是有状态追踪的。这样Entity Framework才能正常增,删,改。git

有时候,要根据某个字段,批量更新或者删除数据,用Entity Framework就会显得非常繁琐,且不高效。github

     Entity Framework Plus 为Entity Framework 提供 BatchUpdate 和 BatchDelete 操做扩展。使得更新和删除数据,变得简单而高效了许多。ide

废话很少说,直接实践给你们看。工具

    一. 建立项目以及相关代码展现,仍是以前的解决方案 “EntityFrameworkPlusSolution”。布局

 1. 在解决方案,新增”EntityFrameworkPlus.BatchOperations.Demo“ WinForm 项目。学习

 在项目中分别新增 “BatchOperations”,“BatchUpdate”,“BatchDelete”  窗口,每一个窗口布局和代码以下。测试

BatchOperations (BatchUpdate,BatchDelete 窗口的入口)spa

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(); } } }
View Code

BatchUpdate 3d

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(); } } }
View Code

 

BatchDeletecode

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(); } } }
View Code

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

相关文章
相关标签/搜索