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 = ; System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start(); for (int i = ; i < count; i++)
{
var userInfoEntity = new UserInfoEntity()
{
Id = Guid.NewGuid(),
LoginName = $"登录名{i.ToString("")}",
LoginPassword = $"登录密码{i.ToString("")}",
Name = $"姓名{i.ToString("")}",
Mobile = $"手机号{i.ToString("")}",
Role = $"角色{i.ToString("")}"
};
this._UserInfoRepository.AddBulkInsertTest(userInfoEntity);
} this._UserInfoContext.SaveChanges(); watch.Stop();
result = watch.Elapsed.TotalSeconds; return result;
} public double BatchAddBulkInsertTest(int count)
{
double result = ; System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start(); var bulkInsertTests = new List<UserInfoEntity>();
for (int i = ; i < count; i++)
{
var userInfoEntity = new UserInfoEntity()
{
Id = Guid.NewGuid(),
LoginName = $"登录名{i.ToString("")}",
LoginPassword = $"登录密码{i.ToString("")}",
Name = $"姓名{i.ToString("")}",
Mobile = $"手机号{i.ToString("")}",
Role = $"角色{i.ToString("")}"
};
bulkInsertTests.Add(userInfoEntity);
} this._UserInfoRepository.BatchAddBulkInsertTest(bulkInsertTests); watch.Stop();
result = watch.Elapsed.TotalSeconds; return result;
} public double UpdateBulkInsertTest()
{
double result = ; 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 = ; 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 = ; System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start(); this._UserInfoRepository.BatchUpdateBulkInsertTest(o => true, new UserInfoEntity()
{
Name = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
Mobile = ""
}, new string[] { "Name" }.ToList()); watch.Stop();
result = watch.Elapsed.TotalSeconds; return result;
} public double DeleteBulkInsertTest()
{
double result = ; 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 = ; 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的更多相关文章
- ASP.NET Core 2.2 迁移至 3.0 备忘录
将 ASP.NET Core 2.2 迁移至 ASP.NET Core 3.0 需要注意的地方记录在这篇随笔中. TargetFramework 改为 netcoreapp3.0 <Target ...
- EF Core扩展工具记录
Microsoft.EntityFrameworkCore.AutoHistory Microsoft.EntityFrameworkCore 的一个插件,支持自动记录数据更改历史记录. GitHub ...
- ABP 框架集成EF批量增加、删除、修改只针对使用mmsql的
AppService 层使用nuget 添加 EFCore.BulkExtensions 引用 using Abp.Application.Services.Dto; using Abp.Domain ...
- 一系列令人敬畏的.NET核心库,工具,框架和软件
内容 一般 框架,库和工具 API 应用框架 应用模板 身份验证和授权 Blockchain 博特 构建自动化 捆绑和缩小 高速缓存 CMS 代码分析和指标 压缩 编译器,管道工和语言 加密 数据库 ...
- ASP.NET Core 2.2 项目升级至 3.0 备忘录
将 ASP.NET Core 2.2 迁移至 ASP.NET Core 3.0 需要注意的地方记录在这篇随笔中. TargetFramework 改为 netcoreapp3.0 <Target ...
- Github上优秀的.NET Core项目
Github上优秀的.NET Core开源项目的集合.内容包括:库.工具.框架.模板引擎.身份认证.数据库.ORM框架.图片处理.文本处理.机器学习.日志.代码分析.教程等. Github地址:htt ...
- 【转载】Github上优秀的.NET Core项目
Github上优秀的.NET Core项目 Github上优秀的.NET Core开源项目的集合.内容包括:库.工具.框架.模板引擎.身份认证.数据库.ORM框架.图片处理.文本处理.机器学习.日志. ...
- 单表千万行数据库 LIKE 搜索优化手记
我们经常在数据库中使用 LIKE 操作符来完成对数据的模糊搜索,LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. 如果需要查找客户表中所有姓氏是“张”的数据,可以使用下面的 SQL 语句 ...
- EF Core扩展工具记录 批量操作 记录修改删除历史 动态linq
Microsoft.EntityFrameworkCore.UnitOfWork Microsoft.EntityFrameworkCore的插件,用于支持存储库,工作单元模式以及支持分布式事务 ...
随机推荐
- ubuntu apt 换源
修改配置文件/etc/apt/sources.list 内容替换为 阿里镜像源 deb http://mirrors.aliyun.com/ubuntu/ vivid main restricted ...
- 在docker上部署centos
1.查找镜像源$ docker search centosNAME DESCRIPTION STARS OFFICIALcentos The official build of CentOS. 385 ...
- 实例:通过调用外部程序进行录制视频(ffmpeg.exe)
相关知识点: 1. ffmpeg可以用下面的参数来录制Windows 桌面操作的视频. ffmpeg.exe -y -rtbufsize 100M -f gdigrab -framerate 10 - ...
- layer iframe 设置关闭按钮 和刷新
layer.open({ type: 2, title: 'XXXX网吧历史更多数据', shade:0, // closeBtn:0, resize:false, move:false, shade ...
- jQuery尺寸
jQuery 尺寸 jQuery width() 和 height() 方法 width() 方法设置或返回元素的宽度(不包括内边距.边框或外边距). height() 方法设置或返回元素的高度(不包 ...
- 高端OLED电视成行业突破口,苏宁助力显示技术市场迭代
编辑 | 于斌 出品 | 于见(mpyujian) 在电商与线上渠道越来越占据举足轻重地位的年代,电商平台巨头们越来越有底气喊出"推动行业技术升级"的口号.的确,再好的技术升级也需 ...
- Activiti+Shiro实战
有人曾说:人的差距都在业余时间拉开的……嗯,我现在深刻理解着这句话,作为一个程序员,技术男,就得不断学习新的技术,跟上时代步伐,才会让自己更有价值~~~~以下这个项目是个人利用业余时间学习并实践的~如 ...
- js面向对象的程序设计 --- 中篇(创建对象) 之 工厂模式和 构造函数模式
创建对象 虽然Object构造函数或对象字面量都可以用来创建单个对象,但这些方式有个明显的缺点:使用同一个接口创建很多对象,会产生大量重复代码. ·工厂模式 工厂模式是一种广为人知的设计模式,这种模式 ...
- LCA 倍增算法模板
. #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm&g ...
- Docker 进入正在运行的容器的4种方式
在使用Docker创建了容器之后,如何进入该容器呢? 进入Docker容器比较常见的几种做法如下: 使用docker attach 使用SSH 使用nsenter 使用exec 一.使用docker ...