abp.NET core)+easyui+efcore 实现仓储管理系统目录
abp.NET core)+easyui+efcore 实现仓储管理系统 --ABP 总体介绍(一)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 解决方案介绍(二)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 领域层创建实体(三)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 定义仓储并实现 (四)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 创建应用服务(五)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 展现层实现增删改查之控制器(六)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 展现层实现增删改查之列表视图(七)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 展现层实现增删改查之增删改视图(八)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 展现层实现增删改查之菜单与测试(九)
abp.NET core)+easyui+efcore 实现仓储管理系统 -- 多语言(十)
通过 abp.NET core)+easyui+efcore 实现仓储管理系统 -- 展现层实现增删改查之控制器 (六) 至 abp.NET core)+easyui+efcore 实现仓储管理系统 -- 展现层实现增删改查之菜单与测试 (九) 四篇文章的学习, 我们使用 ASP.NET Core Mvc 的常规的实现方式实现了对数据库的 CURD 操作. ABP 有其默认的实现增删改查的方式. 我们可以先看一下 "ABP.TPLMS.web.Mvc" 项目中的 "Views\Users" 的相关代码, 可以查看一下 ABP 默认是如何实现对用户信息的增删改查的. 我们发现 ABP 中的用户信息的增删改查是通过继承 AsyncCrudAppService 这个类来实现 CURD 操作, 前端页面中通过 JavaScript 调用 Web API 来实现增删改查. 当然还有一个同步操作类 CrudAppService, 通过继承这个类来实现 CURD 的同步操作. 对于这两个类的的区别在于 AsyncCrudAppService 是 CrudAppService 异步实现. ABP 作为开发框架, 通过以上两个基类实现了对于 CRUD 这种通用功能的一种解决方案. 在接下来的几篇文章中, 我们要通过继承 AsyncCrudAppService 这个类来实现 CURD 操作, 在前端通过调用 WebAPI 来实现对供应商信息的增删改查功能.
先来看一下 AsyncCrudAppService 与 CrudAppService 这两个类具体提供的功能.
首先, 这两个类都继承自 CrudAppServiceBase 类. 如图 1, 图 2.
图 1
图 2
其次, 这两个类都提供了 Create,Delete,Update,Get,GetAll,GetEntityById 方法.
第三, CrudAppServiceBase 类提供了有关于权限 (xxxPermissionName 属性和 CheckxxxPermission 方法) 的属性和方法, 关于分页 (ApplyPaging) 的方法, 关于排序 (ApplySorting) 方法, 关于查询条件 (CreateFilteredQuery) 的方法, 关于对象映射 (MapToxxx) 的方法. 如下图.
接下来我们来通过实现一个供应商信息的管理功能来学习一下这种方式实现增删改查功能. 我会通过几篇文章来一步一步来实现这个供应商管理的功能.
一, 创建 Supplier 实体
1. 在 Visual Studio 2017 的 "解决方案资源管理器" 中, 右键单击 "ABP.TPLMS.Core" 项目的 "Entitys" 文件夹, 在弹出菜单中选择 "添加" --> "类". 将类命名为 Supplier, 然后选择 "添加".
2. 创建 Supplier 类继承自 Entity<int>, 通过实现审计模块中的 IHasCreationTime 来实现保存创建时间. 代码如下:
- using Abp.Domain.Entities;
- using Abp.Domain.Entities.Auditing;
- using Abp.Timing;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Text;
- namespace ABP.TPLMS.Entitys
- {
- public class Supplier : Entity<int>, IHasCreationTime
- {
- public const int MaxLength = 255;
- public Supplier()
- {
- this.Address = string.Empty;
- this.Name = string.Empty;
- this.Email = string.Empty;
- this.Code = string.Empty;
- this.Sex = 0;
- this.LinkName = string.Empty;
- this.Status = 0;
- this.Tel = string.Empty;
- this.Mobile = string.Empty;
- this.UserId = 0;
- CreationTime = Clock.Now;
- }
- [Required]
- [StringLength(50)]
- public string Code { get; set; }
- [Required]
- [StringLength(MaxLength)]
- public string Name { get; set; }
- [StringLength(MaxLength)]
- public string Address { get; set; }
- [Required]
- [StringLength(MaxLength)]
- public string Email { get; set; }
- [StringLength(MaxLength)]
- public string LinkName { get; set; }
- public int Sex { get; set; }
- [Required]
- [StringLength(MaxLength)]
- public string Tel { get; set; }
- [StringLength(MaxLength)]
- public string Mobile { get; set; }
- public int Status { get; set; }
- public int UserId { get; set; }
- public DateTime CreationTime { get; set; }
- }
- }
3. 定义好实体之后, 我们去 "ABP.TPLMS.EntityFrameworkCore" 项目中的 "TPLMSDbContext" 类中定义实体对应的 DbSet, 以应用 Code First 数据迁移. 添加以下代码
- using Microsoft.EntityFrameworkCore;
- using Abp.Zero.EntityFrameworkCore;
- using ABP.TPLMS.Authorization.Roles;
- using ABP.TPLMS.Authorization.Users;
- using ABP.TPLMS.MultiTenancy;
- using ABP.TPLMS.Entitys;
- namespace ABP.TPLMS.EntityFrameworkCore
- {
- public class TPLMSDbContext : AbpZeroDbContext<Tenant, Role, User, TPLMSDbContext>
- {
- /* Define a DbSet for each entity of the application */
- public TPLMSDbContext(DbContextOptions<TPLMSDbContext> options)
- : base(options)
- {
- }
- public DbSet<Module> Modules { get; set; }
- public DbSet<Supplier> Suppliers { get; set; }
- }
- }
4. 从菜单中选择 "工具 ->NuGet 包管理器器 ->程序包管理器控制台" 菜单.
5. 在 PMC 中, 默认项目选择 EntityframeworkCore 对应的项目后. 输入以下命令: Add-Migration AddEntitySupplier, 创建迁移. 如下图.
6. 在上面的命令执行完毕之后, 创建成功后, 会在 Migrations 文件夹下创建时间_AddEntitySupplier 格式的类文件, 这些代码是基于 DbContext 指定的模型. 如下图.
7. 在程序包管理器控制台, 输入 Update-Database, 回车执行迁移. 如下图.
8. 执行成功后, 查看数据库, Suppliers 表创建成功.
来源: https://www.cnblogs.com/chillsrc/p/11269737.html