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