EFCore.BulkExtensions Demo
Posted jasonlai2016
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<UserInfoEntity>() .FirstOrDefault(o => o.LoginName.Equals(loginName) && o.LoginPassword.Equals(loginPassword)); } public UserInfoEntity GetUserInfo(Guid id) { var currentContext = this.context as UserInfoContext; return currentContext.Set<UserInfoEntity>().FirstOrDefault(o => o.Id == id); } public void UpdateUserInfo(UserInfoEntity userInfoEntity) { var currentContext = this.context as UserInfoContext; currentContext.Entry<UserInfoEntity>(userInfoEntity).State = EntityState.Modified; } #endregion #region BulkExtensions应用 /// <summary> /// 插入单个数据 /// </summary> /// <param name="bulkInsertTest"></param> public void AddBulkInsertTest(UserInfoEntity bulkInsertTest) { var currentContext = this.context as UserInfoContext; currentContext.UserInfoEntity.Add(bulkInsertTest); } /// <summary> /// 批量插入数据 /// </summary> /// <param name="bulkInsertTests"></param> public void BatchAddBulkInsertTest(List<UserInfoEntity> bulkInsertTests) { var currentContext = this.context as UserInfoContext; currentContext.BulkInsert(bulkInsertTests); } /// <summary> /// 批量更新数据(更新所有字段) /// </summary> /// <param name="conditionExpression"></param> /// <param name="updateExpression"></param> public void BatchUpdateBulkInsertTest(Expression<Func<UserInfoEntity, bool>> conditionExpression, Expression<Func<UserInfoEntity, UserInfoEntity>> updateExpression) { var currentContext = this.context as UserInfoContext; currentContext.UserInfoEntity.Where(conditionExpression).BatchUpdate(updateExpression); } /// <summary> /// 批量更新数据(更新指定字段) /// </summary> /// <param name="conditionExpression"></param> /// <param name="updateValue"></param> /// <param name="updateColumns"></param> public void BatchUpdateBulkInsertTest(Expression<Func<UserInfoEntity, bool>> conditionExpression, UserInfoEntity updateValue, List<string> updateColumns = null) { var currentContext = this.context as UserInfoContext; currentContext.UserInfoEntity.Where(conditionExpression).BatchUpdate(updateValue, updateColumns); } /// <summary> /// 删除单个数据 /// </summary> /// <param name="bulkInsertTest"></param> public void DeleteBulkInsertTest(UserInfoEntity bulkInsertTest) { var currentContext = this.context as UserInfoContext; currentContext.UserInfoEntity.Remove(bulkInsertTest); } /// <summary> /// 批量删除 /// </summary> /// <param name="conditionExpression"></param> public void BatchDeleteBulkInsertTest(Expression<Func<UserInfoEntity, bool>> 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<UserInfoEntity>(); 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的主要内容,如果未能解决你的问题,请参考以下文章