最近在学习ABP框架,打算通过一个简单的增删改查示例来快速入门,厘清整个框架的层次逻辑。
进入这里下载ABP启动模板:
选择 Multi Page Web Application
创建项目
解压下载好的压缩包,使用 visual studio
打开解决方案(即College.sln文件)
ABP 项目初始化后默认使用的是 SQL Server
数据库,要使用 MySQl
数据库需要进行一些配置。
Add-Migration "AbpZero_Initial" Update-Database "AbpZero_Initial"
出现下图的信息则意味成功:
打开数据库,可以发现数据库已经创建
200
说明请求成功在领域层(即College.Core)下创建文件夹 Entities, 用于存放实体
在Entities下创建实体类 Course.cs 用于创建课程信息
using Abp.Domain.Entities; using Abp.Domain.Entities.Auditing; using Abp.Timing; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace College.Entities { public class Course : Entity<int>, IHasCreationTime { public Course () { this.Code = string.Empty; this.DepartmentCode = string.Empty; this.Name = string.Empty; this.Credits = 0; this.Description = string.Empty; this.Status = 0; this.CreateDate = null; this.CreateName = string.Empty; this.UpdateDate = null; this.UpdateName = string.Empty; this.CreationTime = Clock.Now; } /// <summary> /// 课程编号 /// </summary> [StringLength(50)] public string Code { get; set; } /// <summary> /// 院系编号 /// </summary> [StringLength(50)] public string DepartmentCode { get; set; } /// <summary> /// 课程名称 /// </summary> [StringLength (150)] public string Name { get; set; } /// <summary> /// 课程积分 /// </summary> [Range(0, 5)] public int Credits { get; set; } /// <summary> /// 备注 /// </summary> [StringLength(200)] public string Description { get; set; } /// <summary> /// 状态 /// </summary> public int? Status { get; set; } /// <summary> /// 创建日期 /// </summary> public DateTime? CreateDate { get; set; } /// <summary> /// 创建人 /// </summary> [StringLength(50)] public string CreateName { get; set; } /// <summary> /// 修改日期 /// </summary> public DateTime? UpdateDate { get; set; } /// <summary> /// 修改人 /// </summary> [StringLength(50)] public string UpdateName { get; set; } public DateTime CreationTime { get; set; } } }
在基础设施层(即College.EntityFrameworkCore\EntityFrameworkCore\CollegeDbContext.cs)添加一行 public DbSet<Course> courses { get; set; } // 创建 courses 表
using Microsoft.EntityFrameworkCore; using Abp.Zero.EntityFrameworkCore; using College.Authorization.Roles; using College.Authorization.Users; using College.MultiTenancy; using College.Entities; namespace College.EntityFrameworkCore { public class CollegeDbContext : AbpZeroDbContext<Tenant, Role, User, CollegeDbContext> { /* Define a DbSet for each entity of the application */ public CollegeDbContext(DbContextOptions<CollegeDbContext> options) : base(options) { } public DbSet<Course> courses { get; set; } } }
打开程序包管理工具,项目选择 EntityFrameworkCore,依次执行下列命令
Add-Migration 'AddCourse' Update-Database -Verbose
看到数据库已经有 course
表则说明创建成功