EF学习基本增删查改

1,首先你需要创建好实体和入参的DTO(这一步骤省略,自己创建)

2, 然后就是在你的Service中编写代码(逻辑层)

添加数据

 1 public async Task<bool> AddFeedback(FeedbackDTO dto)
 2 {
 3             var Questi = _mapper.Map<Feedback>(dto);
 4             await using var tran = await _context.Database.BeginTransactionAsync();
 5             try
 6             {
 7                 await _context.Feedback.AddAsync(Questi);
 8                 var res = await _context.SaveChangesAsync();
 9                 await tran.CommitAsync();
10                 return res > 0;
11             }
12             catch (Exception e)
13             {
14                 //Console.WriteLine(e);
15                 await tran.RollbackAsync();
16                 return false;
17             }
18         }
View Service

查询数据

 1 public async Task<CommonListDTO<FeedbackListDTO>> GetFeedbackListList(Expression<Func<FeedbackListDTO, bool>> @where, int PageIndex, int PageSize)
 2         {
 3             var userWhere = _mapper.MapExpression<Expression<Func<Feedback, bool>>>(@where);
 4             var queryList = from a in _context.Feedback.Where(userWhere).DefaultIfEmpty()
 5                             select new FeedbackListDTO
 6                             {
 7                                 Id = a.Id,
 8                                 MainId = a.MainId,
 9                                 OpinionContent = a.OpinionContent,
10                                 Title = a.Title,
11                                 CreateTime = a.CreateTime,
12                                 OpinionState = a.OpinionState,
13                                 LastModiTime = a.LastModiTime,
14                                 IsDel = a.IsDel,
15                                 ReplyContent = a.ReplyContent,
16                                 ReplyDate = a.ReplyDate
17                             };
18             int count = await queryList.CountAsync();
19 
20             var list = await queryList.OrderByDescending(c => c.CreateTime).Skip(PageSize * (PageIndex - 1)).Take(PageSize).ToListAsync();
21 
22             CommonListDTO<FeedbackListDTO> Dot = new CommonListDTO<FeedbackListDTO>()
23             {
24                 Count = count,
25                 List = list
26             };
27             return Dot;
28         }
View Code

删除数据 || 修改数据

 1 public async Task<int> ModifyFeedbackById(FeedbackDTO Fee)
 2         {
 3             Feedback dto = _mapper.Map<FeedbackDTO, Feedback>(Fee);
 4 
 5             try
 6             {
 7                 _context.Feedback.Update(dto);
 8                 return await _context.SaveChangesAsync();
 9             }
10             catch (Exception ex)
11             {
12 
13                 throw;
14             }
15 
16         }
View Code

 

控制器中的编写

 1 [HttpPost(nameof(GetMessageInfoAll))]
 2         public async Task<ResultWithNoData> GetMessageInfoAll(AdminGetMessageInfoDateModel Mes) 
 3         {
 4             ResultWithNoData res = new ResultWithNoData(false, "查询失败", null);
 5 
 6             try
 7             {
 8                 Expression<Func<MessageInfoListDTO, bool>> exception = exception => 1 == 1;
 9 
10                 if (Mes.StartTime.HasValue)
11                 {
12                     exception = exception.And(x => x.CreateTime.Date >= Mes.StartTime.Value);
13                 }
14 
15                 if (Mes.EndTime.HasValue)
16                 {
17                     exception =  exception.And(x => x.CreateTime.Date <= Mes.EndTime.Value);
18                 }
19 
20                 if (!string.IsNullOrWhiteSpace(Mes.Content))
21                 {
22                     exception = exception.And(x => x.MessageTitle.Contains(Mes.Content));
23                 }
24 
25                 var date = await MessageInfoService.GetMessageInfoList(exception, Mes.PageIndex, Mes.PageSize);
26 
27                 res.Data = date;
28                 res.Res = true;
29                 res.Msg = "查询成功";
30             }
31             catch (Exception ex)
32             {
33                 res.Res = false;
34                 res.Msg = ex.Message;
35             }
36             return res;
37         }
View Code

 

posted @ 2020-11-30 09:28  阿华~  阅读(73)  评论(2)    收藏  举报