ABP框架增删改查(统一格式 增删改)基础

本次给大家分享ABP框架详细的增删改这里采用的是统一返回格式

 我们在ABP框架中EntityFrameworkCore文件夹下的LowCodeDevelopmentDbContext配好我们的DbSet(可以暂时理解为上下文)

DbSet:表示可用于创建,读取,更新和删除操作的实体集。

ReturnCode是返回状态是否为200成功还是说404等系列状态

Date为返回内容我们可把返回值直接赋给Date即可如返回失败则为空(null)

ErrorMessage为返回的错误信息为什么

IsSuccess 为操作是否成功

(ToTaK这里用不到是我后续做操作所用)

我们建一个接口,在里面写好我们要操作的类型 例如添加(ADD) unify类型(unify)是上一个统一返回格式的类名

建一个类继承我们刚才的接口,把想要操作的数据注入进去

我们最重要的是要找到我们具体要操作的ID 

其中InsertAsync是做一个添加的操作,我们只要把我们配好的上下文.出来这个InsertAsync放入我们要添加好的表就可以

查询: GetListAsync

修改:UpdateAsync

综上所述我们只需要上下文点出我们对应我们要做的操作即可

这个是删除,我们只需查到我们相应要操作的数据Result是结果的意思我们用 Lambda表达式找到我们的ID直接删除返回即可

注:以前我们把数据拿过来还要Nuw一下现在我们注入是相应的那个数据ABP自动为我们配好了上下文我们直接点就可以)

欢迎大家指导  积极听教

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
ABP框架提供了通用的增删(CRUD)操作,如果需要自定义操作,可以按照以下步骤进行: 1. 定义自定义服务方法 在应用服务接口中定义自定义方法,例如: ```csharp public interface IStudentAppService : IApplicationService { Task<ListResultDto<StudentDto>> GetAllStudents(); Task<StudentDto> GetStudentById(int id); Task CreateStudent(CreateStudentInput input); Task UpdateStudent(UpdateStudentInput input); Task DeleteStudent(int id); Task MyCustomMethod(MyCustomMethodInput input); } ``` 其中,MyCustomMethod 为自定义方法。 2. 实现自定义服务方法 在应用服务实现类中实现自定义方法,例如: ```csharp public class StudentAppService : ApplicationService, IStudentAppService { private readonly IRepository<Student> _studentRepository; public StudentAppService(IRepository<Student> studentRepository) { _studentRepository = studentRepository; } public async Task<ListResultDto<StudentDto>> GetAllStudents() { var students = await _studentRepository.GetAllListAsync(); return new ListResultDto<StudentDto>(ObjectMapper.Map<List<StudentDto>>(students)); } public async Task<StudentDto> GetStudentById(int id) { var student = await _studentRepository.GetAsync(id); return ObjectMapper.Map<StudentDto>(student); } public async Task CreateStudent(CreateStudentInput input) { var student = ObjectMapper.Map<Student>(input); await _studentRepository.InsertAsync(student); } public async Task UpdateStudent(UpdateStudentInput input) { var student = await _studentRepository.GetAsync(input.Id); ObjectMapper.Map(input, student); } public async Task DeleteStudent(int id) { await _studentRepository.DeleteAsync(id); } public async Task MyCustomMethod(MyCustomMethodInput input) { // 实现自定义方法 } } ``` 其中,MyCustomMethod 实现自定义方法的逻辑。 3. 在前端代码中调用自定义服务方法 在前端代码中调用自定义服务方法,例如: ```javascript abp.services.app.student.myCustomMethod(input).done(function (result) { // 处理自定义方法的返回结果 }); ``` 其中,input 为传递给自定义方法的参数。 通过以上步骤,即可完成自定义增删操作。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值