创建实体
在领域层
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));
}
}
注意
- 接口命名对应restful风格
- Get开头GET方法
- Update开头PUT方法
- Delete或Remove开头DELETE方法
- 其余都是POST方法