修改MyProject.EntityFrameworkCore项目的/EntityFrameworkCore/MyProjectDbContext:
public class MyProjectDbContext : AbpZeroDbContext<Tenant, Role, User, MyProjectDbContext> { /* Define a DbSet for each entity of the application */ public DbSet<Simple> Simples { get; set; } public MyProjectDbContext(DbContextOptions<MyProjectDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Simple>(p => { p.ToTable("Simples", "test"); p.Property(x => x.Name).IsRequired(true).HasMaxLength(20); p.Property(x => x.Details).HasMaxLength(100); }); } }然后就可以迁移数据库了,程序包管理器控制台执行:add-migration mytest1,update-database
刷新数据库,Simples表已生成:
实体的增删改查
进入MyProject.Application项目,新建一个MyTest文件夹
CreateSimpleDto,新增Simple数据的传输对象,比如ID,创建时间,创建人等字段,就可以省略
public class CreateSimpleDto { public string Name { get; set; } public string Details { get; set; } }PagedSimpleResultRequestDto,分页查询对象
public class PagedSimpleResultRequestDto : PagedResultRequestDto { /// <summary> /// 查询关键字 /// </summary> public string Keyword { get; set; } }SimpleDto,这里跟CreateSimpleDto的区别就是继承了EntityDto,多了个ID属性
public class SimpleDto : EntityDto<int> { public string Name { get; set; } public string Details { get; set; } }SimpleProfile,用来定义AutoMapper的映射关系清单
public class SimpleProfile : Profile { public SimpleProfile() { CreateMap<Simple, SimpleDto>(); CreateMap<SimpleDto, Simple>(); CreateMap<CreateSimpleDto, Simple>(); } } Service注意,类名参考abp的规范去命名。
ISimpleAppService,Simple服务接口。我这里继承IAsyncCrudAppService,这个接口中包含了增删改查的基本定义,非常方便。如果不需要的话,也可以继承IApplicationService自己定义
public interface ISimpleAppService : IAsyncCrudAppService<SimpleDto, int, PagedSimpleResultRequestDto, CreateSimpleDto, SimpleDto> { }SimpleAppService,Simple服务,继承包含了增删改查的AsyncCrudAppService类,如果有需要的话可以override这些增删改查方法。也可以继承MyProjectAppServiceBase,自己定义。
public class SimpleAppService : AsyncCrudAppService<Simple, SimpleDto, int, PagedSimpleResultRequestDto, CreateSimpleDto, SimpleDto>, ISimpleAppService { public SimpleAppService(IRepository<Simple, int> repository) : base(repository) { } /// <summary> /// 条件过滤 /// </summary> /// <param></param> /// <returns></returns> protected override IQueryable<Simple> CreateFilteredQuery(PagedSimpleResultRequestDto input) { return Repository.GetAll() .WhereIf(!input.Keyword.IsNullOrWhiteSpace(), a => a.Name.Contains(input.Keyword)); } } 接口测试重新运行项目,不出意外的话,Swagger中就会多出Simple相关的接口。
Create
Get
GetAll