欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十一)

程序员文章站 2022-07-01 16:19:47
通过abp(net core)+easyui+efcore实现仓储管理系统——展现层实现增删改查之控制器(六)至abp(net core)+easyui+efcore实现仓储管理系统——展现层实现增删改查之菜单与测试(九)四篇文章的学习,我们使用ASP.NET Core Mvc的常规的实现方式实现了... ......

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实现仓储管理系统——多语言(十)

 

      通过四篇文章的学习,我们使用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。

 abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十一)

图1

 abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十一)

图2

     其次,这两个类都提供了create、delete、update、get、getall、getentitybyid方法。

      第三,crudappservicebase类提供了有关于权限(xxxpermissionname属性和checkxxxpermission方法)的属性和方法,关于分页(applypaging)的方法,关于排序(applysorting)方法,关于查询条件(createfilteredquery)的方法,关于对象映射(maptoxxx)的方法。如下图。 abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十一)

        接下来我们来通过实现一个供应商信息的管理功能来学习一下这种方式实现增删改查功能。我会通过几篇文章来一步一步来实现这个供应商管理的功能。

 一、创建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,创建迁移。如下图。

 abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十一)

     6. 在上面的命令执行完毕之后,创建成功后,会在migrations文件夹下创建时间_addentitysupplier格式的类文件,这些代码是基于dbcontext指定的模型。如下图。

 abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十一)

     7.在程序包管理器控制台,输入update-database,回车执行迁移。如下图。

 abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十一)

     8.执行成功后,查看数据库,suppliers表创建成功。

abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十一)