在领域层
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
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()
// .HasIndex(b => b.Name)
// .IsUnique();
//modelBuilder.Entity().HasAlternateKey(c => c.Name).HasName("IX_SingeColumn");
//modelBuilder.Entity().HasAlternateKey(c => new[] { c.Name }).HasName("IX_MultipleColumns");
//modelBuilder.ChangeAbpTablePrefix("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 _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));
}
}