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 = ;
public Module()
{ this.DisplayName = string.Empty;
this.Name = string.Empty;
this.Url = string.Empty;
this.HotKey = string.Empty;
this.ParentId = ;
this.IconName = string.Empty;
this.Status = ;
this.ParentName = string.Empty;
this.RequiredPermissionName = string.Empty;
this.RequiresAuthentication = false;
this.SortNo = ; 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表创建成功。如下图。

abp(net core)+easyui+efcore仓储系统——领域层创建实体(三)的更多相关文章

  1. abp(net core)+easyui+efcore仓储系统——展现层实现增删改查之控制器(六)

    abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) abp(net core)+easyui+e ...

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

    abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) ABP框架 首先介绍一下abp框架,abp其 ...

  3. abp(net core)+easyui+efcore仓储系统——定义仓储并实现 (四)

    abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) abp(net core)+easyui+e ...

  4. abp(net core)+easyui+efcore仓储系统——创建应用服务(五)

    abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) abp(net core)+easyui+e ...

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

    在前面我已经介绍了ASP.NET MVC.ASP.NET Razor.WEBAPI等技术.我准备通过一个实践项目来整体应用一下之前介绍的技术.本系列是介绍基于ABP+EasyUI的Web开发框架的形成 ...

  6. 2019年7月16日 abp(net core)+easyui+efcore实现仓储管理系统——多语言(十)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

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

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

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

    core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+easyui+e ...

  9. abp(net core)+easyui+efcore实现仓储管理系统——菜单 (十六)

    系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+easyui+efcore实现仓储管理系统——解决方案介绍(二) ...

随机推荐

  1. windows下安装ffmpeg

    一.下载地址: 网址:https://ffmpeg.org/ 选择Windows版本:https://ffmpeg.org/download.html#build-windows 二.解压安装: 下载 ...

  2. 至HDFS附加内容

    在最近的项目开发中遇到的问题: 需要产生良好hdfs文件的其他内容.但使用在线版1.0.3.见发现官方文件,于1.0.4支持的文件的版本号之后append 一下是向hdfs中追加信息的操作方法 假设你 ...

  3. WPF:拖动父窗口行为

    原文 WPF:拖动父窗口行为 这次只是一个快速的帖子:当我点击并拖动特定的UIElement时,我需要能够重新定位WPF窗口.目的是重新创建在标准Windows标题栏上单击和拖动的行为(在我的情况下, ...

  4. respondsToSelector的作用

    1.respondsToSelector 用来推断某一个方法时候实现(以下的代码意思:假设baseAPIdidStartRequest这种方法实现了,那么就去调用,防止出现异常) if ([self. ...

  5. 3ds Max建模,Blend设计,VS2008控制WPF的3D模型例子

    原文:3ds Max建模,Blend设计,VS2008控制WPF的3D模型例子 3ds Max建模,Blend设计,VS2008控制WPF的3D模型例子   所用的软件 3ds Max 9.0,Mic ...

  6. 解决popup不随着window一起移动的问题

    原文:解决popup不随着window一起移动的问题 当我们设置Popup的StayOpen="True"时,会发现移动窗体或者改变窗体的Size的时候,Popup并不会跟随着一起 ...

  7. 【转】CefSharp 与 js 相互调用

    转自CSDN博客博主ghui,虽然博主说要经过他同意才能转,我只是做笔记用,没做他用,所以请博主理解,在此感谢博主! 一. CefSharp调用 js CefSharp.WinForms.Chromi ...

  8. CentOS6.5优盘安装

    从CentOS6.5开始直接把iso文件写入u盘就可实现优盘安装 windows平台:1.用UltraISO打开iso(如:CentOS-6.5-x86_64-bin-DVD1.iso)2.然后点“启 ...

  9. ASP.NET MVC 学习笔记1 Talk about controller & route

    For the sake of learning programming better, I'd like to increase the frequency of using English. So ...

  10. delphi 操作xml示例(DelphiBBS)

    自:http://www.delphibbs.com/keylife/iblog_show.asp?xid=20713 ======================================== ...