使用 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 classItem : 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 設定為啟動項,執行
參考連結