mac 开发abp

创建实体

在领域层

 public class NamePersonFirst : Entity
  {
    public string Name { get; set; }
    public bool IsDouble { get; set; }
    public bool NowSeldom { get; set; }
    public bool AncientSeldom { get; set; }

    public NamePersonFirst()
    {
    }
  }

在应用层创建Dto

[AutoMap(typeof(NamePersonFirst))]
  public class NamePersonFirstDto : EntityDto
  {
    [Required]
    public string Name { get; set; }
    public bool IsDouble { get; set; }
    public bool NowSeldom { get; set; }
    public bool AncientSeldom { get; set; }

    public NamePersonFirstDto()
    {
    }
  }

在EntityFrameworkCore的上下文中添加DbSet

public class AprilDbContext : AbpZeroDbContext<Tenant, Role, User, AprilDbContext>
  {
    /* Define a DbSet for each entity of the application */
    public DbSet<NamePersonFirst> NamePersonFirsts { get; set; }

    public AprilDbContext(DbContextOptions<AprilDbContext> options)
        : base(options)
    {
    }
  }

数据库迁移和更新

运行

cd EntityFrameworkCore层
dotnet ef migrations add "add entity namepersonfirst"
dotnet ef database update

可以在novel.db中看到已生成对应的表了
在这里插入图片描述

CREATE TABLE "NamePersonFirsts" (
	"Id"	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
	"Name"	TEXT NOT NULL,
	"IsDouble"	INTEGER NOT NULL,
	"NowSeldom"	INTEGER NOT NULL,
	"AncientSeldom"	INTEGER NOT NULL
);

表字段约束

我想要的是Name非空唯一。

 protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);
      modelBuilder.Entity<NamePersonFirst>(b =>
      {
        b.HasIndex(e => new { e.Name, e.IsDouble }).IsUnique();
      });
      modelBuilder.Entity<NamePersonFirst>().ToTable("NovelNamePersonFirsts");
    }

虽然表的结构没变,但是增加了索引约束
在这里插入图片描述
其他表述:

      //modelBuilder.Entity<NamePersonFirst>()
      //      .HasIndex(b => b.Name)
      //      .IsUnique();

      //modelBuilder.Entity<NamePersonFirst>().HasAlternateKey(c => c.Name).HasName("IX_SingeColumn");
      //modelBuilder.Entity<NamePersonFirst>().HasAlternateKey(c => new[] { c.Name }).HasName("IX_MultipleColumns");
      //modelBuilder.ChangeAbpTablePrefix<Tenant, Role, User>("Novel");

参考1:Entity Properties
参考2:Code First Data Annotations

 [Column("blog_id")]
 [MaxLength(500)]
 [MaxLength(10),MinLength(5)]
 [Required]
 [EmailAddress]
 [Range(0,1)]
 [ForeignKey("Passport")]
 [Column(Order = 1)]
 [MaxLength(10, ErrorMessage="BloggerName must be 10 characters or less"),MinLength(5)]
 
 [Timestamp]
 public Byte[] TimeStamp { get; set; }
 
 [Table("InternalBlogs")]
 [Column("BlogDescription", TypeName="ntext")]

应用层服务

interface

  public interface INameApplicationService : IApplicationService
  {
    Task<ListResultDto<NamePersonOKDto>> GetNamePersonOKs(NamePersonOKInput input);
  }
  public class NameApplicationService : AprilAppServiceBase, INameApplicationService
  {
    //private readonly IRepository<NamePersonFirst, int> _namePersonFirstRepository;
    private readonly IRepository<NamePersonOK, int> _namePersonOKRepository;

    public NameApplicationService(IRepository<NamePersonOK, int> namePersonOKRepository)
    {
      _namePersonOKRepository = namePersonOKRepository;
    }

    public async Task<ListResultDto<NamePersonOKDto>> GetNamePersonOKs(NamePersonOKInput input)
    {
      var names = await _namePersonOKRepository.GetAllListAsync();
      return new ListResultDto<NamePersonOKDto>(ObjectMapper.Map<List<NamePersonOKDto>>(names));
    }
  }

注意

  1. 接口命名对应restful风格
    • Get开头GET方法
    • Update开头PUT方法
    • Delete或Remove开头DELETE方法
    • 其余都是POST方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值