博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第四篇 Entity Framework Plus 之 Batch Operations
阅读量:4364 次
发布时间:2019-06-07

本文共 4452 字,大约阅读时间需要 14 分钟。

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

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

 

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
> 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://www.cnblogs.com/davidzhou/p/5436624.html

你可能感兴趣的文章
linux下使用dd命令写入镜像文件到u盘
查看>>
物联网架构成长之路(8)-EMQ-Hook了解、连接Kafka发送消息
查看>>
2018-2019-1 20165234 20165236 实验二 固件程序设计
查看>>
IDEA的GUI连接数据库写入SQL语句的问题总结
查看>>
Xpath在选择器中正确,在代码中返回的是空列表问题
查看>>
leecode第一百九十八题(打家劫舍)
查看>>
【BZOJ 1233】 [Usaco2009Open]干草堆tower (单调队列优化DP)
查看>>
07-3. 数素数 (20)
查看>>
写一个欢迎页node统计接口Py脚本(邮件,附件)-py
查看>>
计算两个日期之间的天数
查看>>
Android关于buildToolVersion与CompileSdkVersion的区别
查看>>
袋鼠云日志,日志分析没那么容易
查看>>
缓存穿透 缓存雪崩 缓存并发
查看>>
了解你的Linux系统:必须掌握的20个命令
查看>>
js setInterval 启用&停止
查看>>
knockoutJS学习笔记04:监控属性
查看>>
Linux下启动/关闭Oracle
查看>>
session和cookie的区别
查看>>
alert弹出窗口,点击确认后关闭页面
查看>>
oracle问题之数据库恢复(三)
查看>>