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

AppService 层使用nuget 添加 EFCore.BulkExtensions 引用

 1 using Abp.Application.Services.Dto;
 2 using Abp.Domain.Repositories;
 3 using Abp.EntityFrameworkCore.Repositories;
 4 using Abp.UI;
 5 using EFCore.BulkExtensions;
 6 using Microsoft.EntityFrameworkCore;
 7 using System;
 8 using System.Collections.Generic;
 9 using System.Diagnostics;
10 using System.Linq;
11 using System.Text;
12 using System.Threading.Tasks;
13 using WebProject.Authorization.Accounts;
14 
15 namespace WebProject.Article
16 {
17     public class ArticleAppService : WebProjectAppServiceBase, IArticleAppService
18     {
19         private readonly IRepository<Models.Article> _articleRepository;
20         public ArticleAppService(IRepository<Models.Article> articleRepository)
21         {
22             _articleRepository = articleRepository;
23         }
24         /// <summary>
25         /// 批量添加
26         /// </summary>
27         /// <param name="input"></param>
28         /// <returns></returns>
29         public string BulkInsert(EntityDto input)
30         {
31             Stopwatch watch = new Stopwatch();
32             watch.Start();
33             List<Models.Article> list = new List<Models.Article>();
34             for (int i = 0; i < input.Id; i++)
35             {
36                 var entity = new Models.Article();
37                 entity.CateId = 2;
38                 entity.Title = $"BulkInsert娱乐{1}";
39                 list.Add(entity);
40             }
41            
42             _articleRepository.GetDbContext().BulkInsert(list);//GetDbContext 需要添加Abp.EntityFrameworkCore.Repositories引用
43             watch.Stop();
44            
45             string time = watch.ElapsedMilliseconds.ToString();
46             return time;
47         }
48         /// <summary>
49         /// 批量删除
50         /// </summary>
51         /// <returns></returns>
52         public string BulkDelete()
53         {
54             Stopwatch watch = new Stopwatch();
55             watch.Start();
56             _articleRepository.GetAll().BatchDelete();
57             //_articleRepository.GetAll().Where(m=>m.Id>100).BatchDelete();
58             watch.Stop();
59             string time = watch.ElapsedMilliseconds.ToString();
60             return time;
61         }
62         /// <summary>
63         /// 批量修改
64         /// </summary>
65         /// <returns></returns>
66         public string BatchUpdate()
67         {
68             Stopwatch watch = new Stopwatch();
69             watch.Start();
70             _articleRepository.GetAll().BatchUpdate(new Models.Article { Title = "Updated娱乐" });
71             //_articleRepository.GetAll().Where(m=>m.Id>100).BatchUpdate(new Models.Article { Title = "Updated娱乐" });
72             watch.Stop();
73             string time = watch.ElapsedMilliseconds.ToString();
74             return time;
75         }
76        
77     }
78 }

 

posted on 2019-03-29 16:33  落叶子  阅读(4524)  评论(3编辑  收藏  举报

导航