最近做了一个项目,当用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的更多相关文章

  1. ASP.NET Core 2.2 迁移至 3.0 备忘录

    将 ASP.NET Core 2.2 迁移至 ASP.NET Core 3.0 需要注意的地方记录在这篇随笔中. TargetFramework 改为 netcoreapp3.0 <Target ...

  2. EF Core扩展工具记录

    Microsoft.EntityFrameworkCore.AutoHistory Microsoft.EntityFrameworkCore 的一个插件,支持自动记录数据更改历史记录. GitHub ...

  3. ABP 框架集成EF批量增加、删除、修改只针对使用mmsql的

    AppService 层使用nuget 添加 EFCore.BulkExtensions 引用 using Abp.Application.Services.Dto; using Abp.Domain ...

  4. 一系列令人敬畏的.NET核心库,工具,框架和软件

    内容 一般 框架,库和工具 API 应用框架 应用模板 身份验证和授权 Blockchain 博特 构建自动化 捆绑和缩小 高速缓存 CMS 代码分析和指标 压缩 编译器,管道工和语言 加密 数据库 ...

  5. ASP.NET Core 2.2 项目升级至 3.0 备忘录

    将 ASP.NET Core 2.2 迁移至 ASP.NET Core 3.0 需要注意的地方记录在这篇随笔中. TargetFramework 改为 netcoreapp3.0 <Target ...

  6. Github上优秀的.NET Core项目

    Github上优秀的.NET Core开源项目的集合.内容包括:库.工具.框架.模板引擎.身份认证.数据库.ORM框架.图片处理.文本处理.机器学习.日志.代码分析.教程等. Github地址:htt ...

  7. 【转载】Github上优秀的.NET Core项目

    Github上优秀的.NET Core项目 Github上优秀的.NET Core开源项目的集合.内容包括:库.工具.框架.模板引擎.身份认证.数据库.ORM框架.图片处理.文本处理.机器学习.日志. ...

  8. 单表千万行数据库 LIKE 搜索优化手记

    我们经常在数据库中使用 LIKE 操作符来完成对数据的模糊搜索,LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. 如果需要查找客户表中所有姓氏是“张”的数据,可以使用下面的 SQL 语句 ...

  9. EF Core扩展工具记录 批量操作 记录修改删除历史 动态linq

      Microsoft.EntityFrameworkCore.UnitOfWork  Microsoft.EntityFrameworkCore的插件,用于支持存储库,工作单元模式以及支持分布式事务 ...

随机推荐

  1. Oracle VM VirtualBox - VBOX_E_FILE_ERROR (0x80BB0004)

    问题描述: 导入虚拟电脑 D:\LR\虚拟机相关\CentOS-6.7-x86_64-2G-40G-oracle-IP9\CentOS-6.7-x86_64-2G-40G-oracle-IP9.ovf ...

  2. 认识Flow(一)

    Flow 是 facebook 出品的 JavaScript 静态类型检查工具.Vue.js 的源码利用了 Flow 做了静态类型检查,所以了解 Flow 有助于我们阅读源码. 为什么用 Flow J ...

  3. Python复制指定目录的各个子目录下的同名文件到指定文件夹并重命名

    Python复制指定目录的各个子目录下的同名文件到指定文件夹并重命名 #编码类型 #-*- coding: UTF-8 -*- #导入包 import os import shutil srcpath ...

  4. Java基础面试题总结二

    1,什么是字符串常量池? 字符串的分配,和其他的对象分配一样,耗费高昂的时间与空间代价.JVM为了提高性能和减少内存开销,在实例化字符串常量的时候进行了一些优化.为 了减少在JVM中创建的字符串的数量 ...

  5. ansible笔记(13):变量(二)

    1.谈一谈[Gathering Facts]:使用setup模块查看 当我们运行一个playbook时,默认都会运行一个名为“[Gathering Facts]”的任务,前文中已经大致的介绍过这个默认 ...

  6. Application Comparison Of LED Holiday Light And Traditional Incandescent Lights

    Obviously, LED holiday lights are leading the competition in economical Christmas decorations, but t ...

  7. soundtouch change rate matlab implementation

    soundtouch implement of changing rate in a way same with resample(SRC). When rate < 1, it need in ...

  8. [CF484D] Kindergarten - 贪心

    有一组数,你要把他分成若干连续段.每一段的值,定义为这一段 数中最大值与最小值的差. 求一种分法,使得这若干段的值的和最大. N < 1e6, a[i] < 1e9. 朴素的\(O(n^2 ...

  9. 训练20191009 2018-2019 ACM-ICPC, Asia East Continent Finals

    2018-2019 ACM-ICPC, Asia East Continent Finals 总体情况 本次训练共3小时20分钟,通过题数4. 解题报告 D. Deja vu of - Go Play ...

  10. C分支语句的工程用法

    if语言中零值比较的注意点: -bool型变量应该直接出现于条件中,不要进行比较 -变量和零值比较时,零值应该出现在比较符号左边 -float型变量不能直接进行零值比较,需要定义精度 bool b = ...