用 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