EFCore.BulkExtensions Demo

最近做了一个项目,当用EF传统的方法执行时,花时4小时左右,修改后,时间大大减少到10分钟,下面是DEMO实例

实体代码:

public class UserInfoEntity
    {
        [Key]
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public string LoginName { get; set; }
        public string LoginPassword { get; set; }
        public string Role { get; set; }
    }

仓储代码:

using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace UserInfo
{
    public class UserInfoRepository : IDisposable
    {
        DbContext context;
        public UserInfoRepository(DbContext context)
        {
            this.context = context;
        }
        public void Dispose()
        {
            this.context.Dispose();
        }

        #region 常规EF的方法
        public UserInfoEntity GetUserInfo(string loginName, string loginPassword)
        {
            var currentContext = this.context as UserInfoContext;
            return currentContext.Set()
                .FirstOrDefault(o => o.LoginName.Equals(loginName) && o.LoginPassword.Equals(loginPassword));
        }
        public UserInfoEntity GetUserInfo(Guid id)
        {
            var currentContext = this.context as UserInfoContext;
            return currentContext.Set().FirstOrDefault(o => o.Id == id);
        }

        public void UpdateUserInfo(UserInfoEntity userInfoEntity)
        {
            var currentContext = this.context as UserInfoContext;
            currentContext.Entry(userInfoEntity).State = EntityState.Modified;
        }
        #endregion

        #region BulkExtensions应用
        /// 
        /// 插入单个数据
        /// 
        /// 
        public void AddBulkInsertTest(UserInfoEntity bulkInsertTest)
        {
            var currentContext = this.context as UserInfoContext;
            currentContext.UserInfoEntity.Add(bulkInsertTest);
        }
        /// 
        /// 批量插入数据
        /// 
        /// 
        public void BatchAddBulkInsertTest(List bulkInsertTests)
        {
            var currentContext = this.context as UserInfoContext;
            currentContext.BulkInsert(bulkInsertTests);
        }
        /// 
        /// 批量更新数据(更新所有字段)
        /// 
        /// 
        /// 
        public void BatchUpdateBulkInsertTest(Expressionbool>> conditionExpression, Expression> updateExpression)
        {
            var currentContext = this.context as UserInfoContext;

            currentContext.UserInfoEntity.Where(conditionExpression).BatchUpdate(updateExpression);
        }
        /// 
        /// 批量更新数据(更新指定字段)
        /// 
        /// 
        /// 
        /// 
        public void BatchUpdateBulkInsertTest(Expressionbool>> conditionExpression, UserInfoEntity updateValue, List<string> updateColumns = null)
        {
            var currentContext = this.context as UserInfoContext;
            currentContext.UserInfoEntity.Where(conditionExpression).BatchUpdate(updateValue, updateColumns);
        }
        /// 
        /// 删除单个数据
        /// 
        /// 
        public void DeleteBulkInsertTest(UserInfoEntity bulkInsertTest)
        {
            var currentContext = this.context as UserInfoContext;
            currentContext.UserInfoEntity.Remove(bulkInsertTest);
        }
        /// 
        /// 批量删除
        /// 
        /// 
        public void BatchDeleteBulkInsertTest(Expressionbool>> conditionExpression)
        {
            var currentContext = this.context as UserInfoContext;
            currentContext.UserInfoEntity.Where(conditionExpression).BatchDelete();
        }
        #endregion
    }
}

用例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using UserInfo;
using Util;
using UserInfoDTOInDTO = Business.AppSrv.DTOS.InDTOS.UserInfoDTO;

namespace Business.AppSrv.UseCases
{
    public class UserInfoUseCase : BaseAppSrv, IDisposable
    {
        private UserInfoContext _UserInfoContext;
        private UserInfoRepository _UserInfoRepository;

        public UserInfoUseCase()
        {
            this._UserInfoContext = new UserInfoContext();
            this._UserInfoRepository = new UserInfoRepository(this._UserInfoContext);
        }

        public void Dispose()
        {
            this._UserInfoRepository.Dispose();
        }

        public void UpdateUserLoginPassword(Guid userID, UserInfoDTOInDTO.UserLoginPasswordUpdate userLoginPasswordUpdate)
        {
            if (userLoginPasswordUpdate == null)
            {
                throw new Exception("修改密码参数错误");
            }

            if (string.IsNullOrEmpty(userLoginPasswordUpdate.LoginPasswordOld))
            {
                throw new Exception("旧密码不能为空");
            }
            if (string.IsNullOrEmpty(userLoginPasswordUpdate.LoginPasswordNew))
            {
                throw new Exception("新密码不能为空");
            }

            UserInfoEntity userInfoEntity = this._UserInfoRepository.GetUserInfo(userID);
            if (userInfoEntity == null)
            {
                throw new Exception("用户不存在");
            }

            if (!userLoginPasswordUpdate.LoginPasswordOld.Equals(userInfoEntity.LoginPassword))
            {
                throw new Exception("旧密码错误");
            }
            userInfoEntity.LoginPassword = userLoginPasswordUpdate.LoginPasswordNew;
            this._UserInfoRepository.UpdateUserInfo(userInfoEntity);
            this._UserInfoContext.SaveChanges();
        }
                     
        public double AddBulkInsertTest(int count)
        {
            double result = 0;

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            for (int i = 0; i < count; i++)
            {
                var userInfoEntity = new UserInfoEntity()
                {
                    Id = Guid.NewGuid(),
                    LoginName = $"登录名{i.ToString("00000")}",
                    LoginPassword = $"登录密码{i.ToString("00000")}",
                    Name = $"姓名{i.ToString("00000")}",
                    Mobile = $"手机号{i.ToString("00000")}",
                    Role = $"角色{i.ToString("00000")}"
                };
                this._UserInfoRepository.AddBulkInsertTest(userInfoEntity);
            }

            this._UserInfoContext.SaveChanges();

            watch.Stop();
            result = watch.Elapsed.TotalSeconds;

            return result;
        }

        public double BatchAddBulkInsertTest(int count)
        {
            double result = 0;

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            var bulkInsertTests = new List();
            for (int i = 0; i < count; i++)
            {
                var userInfoEntity = new UserInfoEntity()
                {
                    Id = Guid.NewGuid(),
                    LoginName = $"登录名{i.ToString("00000")}",
                    LoginPassword = $"登录密码{i.ToString("00000")}",
                    Name = $"姓名{i.ToString("00000")}",
                    Mobile = $"手机号{i.ToString("00000")}",
                    Role = $"角色{i.ToString("00000")}"
                };
                bulkInsertTests.Add(userInfoEntity);
            }

            this._UserInfoRepository.BatchAddBulkInsertTest(bulkInsertTests);

            watch.Stop();
            result = watch.Elapsed.TotalSeconds;

            return result;
        }

        public double UpdateBulkInsertTest()
        {
            double result = 0;

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            var list = this._UserInfoContext.UserInfoEntity.Where(o => true).ToList();
            foreach (var item in list)
            {
                item.Name = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            }
            this._UserInfoContext.SaveChanges();

            watch.Stop();
            result = watch.Elapsed.TotalSeconds;

            return result;
        }

        public double BatchUpdateBulkInsertTest0()
        {
            double result = 0;

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            this._UserInfoRepository.BatchUpdateBulkInsertTest(o => true, o => new UserInfoEntity()
            {
                Id = o.Id,
                Name = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                LoginName = o.LoginName,
                LoginPassword = o.LoginPassword,
                Mobile = o.Mobile,
                Role = o.Role
            });

            watch.Stop();
            result = watch.Elapsed.TotalSeconds;

            return result;
        }

        public double BatchUpdateBulkInsertTest1()
        {
            double result = 0;

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            this._UserInfoRepository.BatchUpdateBulkInsertTest(o => true, new UserInfoEntity()
            {
                Name = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                Mobile = "00000"
            }, new string[] { "Name" }.ToList());

            watch.Stop();
            result = watch.Elapsed.TotalSeconds;

            return result;
        }

        public double DeleteBulkInsertTest()
        {
            double result = 0;

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            var list = this._UserInfoContext.UserInfoEntity.Where(o => true).ToList();
            foreach (var item in list)
            {
                this._UserInfoRepository.DeleteBulkInsertTest(item);
            }
            this._UserInfoContext.SaveChanges();

            watch.Stop();
            result = watch.Elapsed.TotalSeconds;

            return result;
        }

        public double BatchDeleteBulkInsertTest()
        {
            double result = 0;

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            this._UserInfoRepository.BatchDeleteBulkInsertTest(o => true);

            watch.Stop();
            result = watch.Elapsed.TotalSeconds;

            return result;
        }
    }
}

 

你可能感兴趣的:(EFCore.BulkExtensions Demo)