ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ABP框架之CRUD简单示例

2022-07-25 15:32:22  阅读:212  来源: 互联网

标签:set 示例 get CRUD ABP EntityFrameworkCore College using public


最近在学习ABP框架,打算通过一个简单的增删改查示例来快速入门,厘清整个框架的层次逻辑。

1. 前期准备

1.1 创建项目

进入这里下载ABP启动模板:
选择 Multi Page Web Application
img
创建项目
img
解压下载好的压缩包,使用 visual studio 打开解决方案(即College.sln文件)
img

1.2 使用 MySQL 数据库

ABP 项目初始化后默认使用的是 SQL Server 数据库,要使用 MySQl 数据库需要进行一些配置。

1.2.1 移除并安装相关套件

  1. 在College.EntityFrameworkCore下移除 xxxx.SqlServer
    img
  2. 在College.EntityFrameworkCore下移除 Microsoft.EntityFrameworkCore.Design
    img
  3. 在College.EntityFrameworkCore下安装 Pomelo.EntityFrameworkCore.MySql
    img
  4. 在College.EntityFrameworkCore下安装 Pomelo.EntityFrameworkCore.MySql.Design
    img
  5. 在College.Web.Host下移除 Microsoft.EntityFrameworkCore.Design
    img
  6. 在College.Web.Host下安装 Microsoft.EntityFrameworkCore.Tools
    img

1.2.2 配置 MySQL 数据库的连接

  1. 按下面三张截图修改数据库连接字符串
    img
    img
    img
  2. 修改College.EntityFrameworkCore下的EntityFrameworkCore文件夹下的CollegeDbContextConfigurer.cs
    img

1.2.3 初始化数据库

  1. 删除College.EntityFrameworkCore下的Migrations文件夹
    img
  2. 设置 College.Web.Host 为启动项
    img
  3. 打开程序包管理控制台,选择EntityFrameworkCore
    img
  4. 依次输入下列命令
Add-Migration "AbpZero_Initial"
Update-Database "AbpZero_Initial"

出现下图的信息则意味成功:
img
打开数据库,可以发现数据库已经创建
img

1.2.4 迁移数据库

  1. 设置College.Migrator为启动项
    img
  2. 运行,输入Y,回车
    img
  3. 大功告成
    img

1.3 启动项目

  1. 设置College.Web.Host为启动项目
  2. 运行,出现下图情况则说明配置成功
    img
  3. 测试一下API
    授权,账号为admin,密码是123qwe
    img
    img
    img
    返回状态码200说明请求成功
    img
  4. 设置College.Web.Mvc为启动项目
  5. 运行,出现下图界面说明整个项目都没有问题
    img
  6. 发现样式不对,是因为缺少libs,按照下图操作,再重新启动就好了
    img
  7. 登录,界面显示如下图,到这里准备工作就完成了
    img
    img

2. 领域层创建实体

在领域层(即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; }
    }
}

3. 基础设施层更新数据库

在基础设施层(即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 表则说明创建成功
img
img

4.

参考

  1. 后端 ABP (ASP.NET Boilerplate) 应用程序开发框架 改用 MySql
  2. ABP入门教程6 - 领域层创建实体

标签:set,示例,get,CRUD,ABP,EntityFrameworkCore,College,using,public
来源: https://www.cnblogs.com/jerry-1015/p/16516240.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有