第四篇 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();
        }
    }
}
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<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

 

posted @ 2016-05-05 09:33  改變世界  阅读(3060)  评论(7编辑  收藏  举报