ABP入门教程(二)添加一个实体和服务

1,在领域层(Core)添加一个实体

    [Table("Department")]
    public class Department : FullAuditedEntity
    {
        [Required]
        [StringLength(64)]
        public string Code { get; set; }

        [Required]
        [StringLength(128)]
        public string Name { get; set; }
    }

2,在基础层(EntityFrameworkCore)的类AbpDemoDbContext添加数据集

    public class Core3DemoDbContext : AbpZeroDbContext
    {
        /* Define a DbSet for each entity of the application */
        public DbSet Departments { get; set; }

        public Core3DemoDbContext(DbContextOptions options)
            : base(options)
        {
        }
    }

3,数据迁移可以在数据库自动建表, 但是个人推荐手动写脚本建表

CREATE TABLE [Department] (
    [Id] int NOT NULL IDENTITY,
    [CreationTime] datetime2 NOT NULL,
    [CreatorUserId] bigint NULL,
    [LastModificationTime] datetime2 NULL,
    [LastModifierUserId] bigint NULL,
    [IsDeleted] bit NOT NULL,
    [DeleterUserId] bigint NULL,
    [DeletionTime] datetime2 NULL,
    [Code] nvarchar(64) NOT NULL,
    [Name] nvarchar(128) NOT NULL,
    CONSTRAINT [PK_Department] PRIMARY KEY ([Id])
);

4,先在应用层(Application)写一个最简单的服务

   public class DepartmentAppService : Core3DemoAppServiceBase
    {
        public string SayHello()
        {
            return "hello";
        }
    }

运行项目打开Swagger我们可以测试这个接口
ABP入门教程(二)添加一个实体和服务_第1张图片

5,在应用层开发增删改查功能

如下图添加Dto 和 Profile 并修改DepartmentAppService类继承AsyncCrudAppService
ABP入门教程(二)添加一个实体和服务_第2张图片
代码分别如下

    public class CreateDepartmentDto
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

    public class DepartmentDto : EntityDto
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

    public class PagedDepartmentResultRequestDto : PagedResultRequestDto
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }
    
    public class DepartmentMapProfile : Profile
    {
        public DepartmentMapProfile()
        {
            CreateMap();
            CreateMap();

            CreateMap();
        }
    }

    public class DepartmentAppService : AsyncCrudAppService
    {
        public DepartmentAppService(IRepository repository) : base(repository)
        {
        }

        public string SayHello()
        {
            return "hello";
        }
    }

运行项目,查询Swagger可以看到增删改查的接口已经有了, 试一下
ABP入门教程(二)添加一个实体和服务_第3张图片

增删改查应该都可以了,如果有错误可以查看Web.Host\App_Data\Logs下面的日志排查问题: 例如没有在数据库建表等等

你可能感兴趣的:(ABP使用入门教程)