使用 abp-cli 创建增删改查项目
1 安装 abp-cli
在 cmd 窗口中执行命令:
dotnet tool install -g Volo.Abp.Cli

2 创建项目文件夹 TodoApp20220525,然后进入 TodoApp20220525 ,在文件地址栏输入cmd命令打开cmd,这样cmd命令的当前路径就为 TodoApp20220525

3 使用 abp-cli 创建项目
执行命令 创建项目名为 TodoApp20220525 使用的模板为 app ,数据库提供程序未为ef,ui为mvc,mobile为none,使用的abp版本为4.4.4
abp new TodoApp20220525 -t app -d ef -u mvc -m none -v 4.4.4

命令执行成功后,创建了如下目录:

4 创建数据库
打开项目解决方案sln文件,修改 项目 TodoApp20220525.DbMigrator 中的 appsettings.json 中的数据库连接配置

然后设置项目 TodoApp20220525.DbMigrator 为启动项目,然后运行项目,运行完后,自动初始化了数据库

5 在 TodoApp20220525.Domain 创建一个 实体 Item
using System;
using Volo.Abp.Domain.Entities;
namespace TodoApp20220525
{
public class Item : BasicAggregateRoot<Guid>
{
public string Text { get; set; }
}
}
6 在数据库上下文中添加 Item 的 DataSet
在项目 TodoApp20220525.EntityFrameworkCore 中的 数据库上下文 TodoApp20220525DbContext 中添加 DbSet 和 实体映射
public DbSet<Item> Items { get; set; }
在 OnModelCreating 方法中添加如下实体映射配置
builder.Entity<Item>(b =>
{
b.ToTable("Item");
});
7 执行数据迁移命令
右击 TodoApp20220525.EntityFrameworkCore 项目,选择 “在文件资源管理器中打开文件夹”,然后在地址栏输入cmd,
输入迁移命令
dotnet ef migrations add Added_Item

然后执行应用迁移的命令
dotnet ef database update

执行成功后,查看数据库,新建了 Item 表

8 在 TodoApp20220525.Application.Contracts 添加应用服务接口和dto
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
namespace TodoApp20220525
{
// 继承 IApplicationService 可以使用动态api
public interface IItemAppService : IApplicationService
{
Task<List<ItemDto>> GetListAsync();
Task<ItemDto> CreateAsync(string text);
Task DeleteAsync(Guid id);
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TodoApp20220525
{
public class ItemDto
{
public Guid Id { get; set; }
public string Text { get; set; }
}
}
9 在 TodoApp20220525.Application 项目中实现应用服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
namespace TodoApp20220525
{
public class ItemAppService : ApplicationService,IItemAppService
{
private readonly IRepository<Item, Guid> _itemRepository;
public ItemAppService(IRepository<Item, Guid> itemRepository)
{
_itemRepository = itemRepository;
}
public async Task<ItemDto> CreateAsync(string text)
{
var insertItem = await _itemRepository.InsertAsync(
new Item { Text = text }
);
return new ItemDto
{
Id = insertItem.Id,
Text = insertItem.Text
};
}
public async Task DeleteAsync(Guid id)
{
await _itemRepository.DeleteAsync(id);
}
public async Task<List<ItemDto>> GetListAsync()
{
var items = await _itemRepository.GetListAsync();
return items
.Select(item => new ItemDto
{
Id = item.Id,
Text = item.Text
}).ToList();
}
}
}
10 修改 TodoApp20220525.Web 项目的 appsettings.json 文件修改数据库连接,然后设置为启动项目运行
点击 login ,输入账号密码 admin/1q2w3E*
如果不存在该用户,就自己注册一个用户
登录成功后,在地址栏加上 /swagger 访问 swagger
测试 新增接口


使用 test 文件夹下的 TodoApp20220525.HttpApi.Client.ConsoleTestApp 访问应用服务,编辑 ClientDemoService 添加使用 IItemAppService 代码如下:

右击 TodoApp20220525.Web 项目,打开文件夹,然后在cmd窗口中执行命令 dotnet run 启动
将 TodoApp20220525.HttpApi.Client.ConsoleTestApp 设置为启动项,运行

转 https://www.cnblogs.com/tomorrow0/p/16307935.html

浙公网安备 33010602011771号