abp(net core)+easyui+efcore仓储系统目录

abp(net core)+easyui+efcore仓储系统——abp总体介绍(一)

abp(net core)+easyui+efcore仓储系统——解决方案介绍(二)

 

      在上二篇文章中我们简单介绍了一下abp.tplms系统的概况,已经对abp的体系结构以及项目结构有了一个初步的了解。在这一篇文章中我们主要和领域层打交道,主要是创建实体与进行迁移。接下来我们开始创建module实体。

一、创建module实体

      实体是ddd(领域驱动设计)的核心概念之一。eirc evans是这样描述的实体的:“它根本上不是通过属性定义的,而是通过一系列连续性和标识定义的”。因此,实体都有id属性并且都存储到数据库中。一个实体一般会映射到数据库的一张表。现在我们来完成以下任务:在领域层创建一个entitys文件夹,并在这个文件夹中创建module实体类。

1. 在visual studio 2017的“解决方案资源管理器”中,右键单击“abp.tplms.core”项目。 选择“添加” > “新建文件夹”。如下图。

2.将文件夹命名为“entitys”。

3. 右键单击“entitys”文件夹,然后选择“添加” > “类”。 将类命名为 module,然后选择“添加”。如下图。

 

4.abp中所有的实体类都继承自entity,而entity实现了ientity接口;而ientity接口是一个泛型接口,通过泛型指定主键id类型,默认的entity的主键类型是int类型。如下图。

 
5.创建module类,肯定需要保存创建时间,可以通过实现审计模块中的ihascreationtime来实现这种通用功能。如下图。

 

6.  abp中实体是派生于entity类,先看一下我们在core层新建的module类。代码如下:

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 module:entity, ihascreationtime
    {

        public const int maxlength = 255;
        public module()
        {

            this.displayname = string.empty;
            this.name = string.empty;
            this.url = string.empty;
            this.hotkey = string.empty;
            this.parentid = 0;
            this.iconname = string.empty;
            this.status = 0;
            this.parentname = string.empty;
            this.requiredpermissionname = string.empty;
            this.requiresauthentication = false;
            this.sortno = 0;         

            creationtime = clock.now;
        }    

        [required]
        [stringlength(maxlength)]
        public string displayname { get; set; }
 

        [required]
        [stringlength(maxlength)]
        public string name { get; set; } 

        [required]
        [stringlength(maxlength)]
        public string url { get; set; }
 

        [stringlength(maxlength)]
        public string hotkey { get; set; }
        public int parentid { get; set; }
        public bool requiresauthentication { get; set; }
        public bool isautoexpand { get; set; } 

        [stringlength(maxlength)]
        public string iconname { get; set; }
        public int status { get; set; } 

        [required]
        [stringlength(maxlength)]
        public string parentname { get; set; }
 

        [stringlength(maxlength)]
        public string requiredpermissionname { get; set; }
        public int sortno { get; set; }           
        public datetime creationtime { get; set; }     
    }
}

      在上面的module实体类中的一些属性上我们定义了[required]、[maxlength]等特性用来进行输入校验的。

      上面的module实体类,没有添加id属性,为什么呢?因为module继承自entity类,entity类已经定义id,它是该entity类的主键。因此,所有继承entity类的实体类的主键名都是id

      id(主键)的类型是可以更改的,默认是int(int32)。如果你想将id定义为其他类型,可以在<>内写,比如guid,long也是可以的。

      entity类重写了等号运算符(==),可以轻松地检查两个实体是否相同了(实体的id相同则认为它们相同)。它也定义了istransient方法来检测它是否有id。 

      ihascreationtime接口使用一个通用的属性来描述一个实体的“创建时间”。当实现了该接口的实体类插入到数据库中时,abp会自动地将当前的时间设置给creationtime。

      7.定义好实体之后,我们就要去dbcontext中定义实体对应的dbset,以应用code first 数据迁移。找到我们的基础服务层,即以entityframeworkcore结尾的项目中,找到dbcontext类,如下图,添加以下代码。

 

 

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; }
    }

}

二、执行code first数据迁移,

    1.从菜单中选择“工具->nuget包管理器器—>程序包管理器控制台”菜单。如下图。

 

    2. 在pmc中,默认项目选择entityframeworkcore对应的项目后。输入以下命令:add-migration addentitymodule,创建迁移。如下图。

 

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

 

4.在程序包管理器控制台,输入update-database,回车执行迁移。执行成功后,查看数据库,moudles表创建成功。如下图。