1. 程式人生 > >abp(net core)+easyui+efcore實現倉儲管理系統——使用 WEBAPI實現CURD (十一)

abp(net core)+easyui+efcore實現倉儲管理系統——使用 WEBAPI實現CURD (十一)

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表建立成功。

 

&n