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. VS2017 Linux 上.NET Core调试

    调试Linux 上.NET Core Visual Studio 2017 通过SSH 调试Linux 上.NET Core 应用程序. 本文环境 开发环境:Win10 x64 Visual Stud ...

  2. Hibernate——(4)Hibernate映射类型

    一.常用的Hibernat映射类型有如下几种: string integer double date    日期,只表示年月日 datetime 日期,只表示年月日 timestamp  时间戳,存放 ...

  3. Winsock出错引起的断网

    背景 实验室有二十来台 PC 机,30+的服务器集群,网络拓扑比较复杂.简单地说,有网关连着校网,校网无法直接访问学校外的网络,只能通过拨 vpn 来实现外网访问.而校网最近也不稳定,时常断网. 今天 ...

  4. Spring Boot中集成Spring Security 专题

    check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...

  5. Method and apparatus for training a memory signal via an error signal of a memory

    Described herein is a method and an apparatus for training a memory signal via an error signal of a ...

  6. Fidder模拟发送请求

    在Fiddler的Composer一栏,可以模拟请求 举例 首先通过浏览器访问页面http://baidu.com/ ,在右侧可以拿到请求情况 在Inspectors一栏可以看到请求和响应结果,复制请 ...

  7. Asp.net MVC Razor输出字符串方法(js中嵌入razor)

    @{ Model p = new Model(); //输出名称和年龄 //1.第一种方式 @:姓名=@p.Name //2.第二中方式 <text>年龄=</text>p.A ...

  8. jquery子元素过滤器

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. js 看图识国家

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  10. telerik ChartGrid浅谈

    在最近接触的项目中,有很多都是以Chart图表的方式呈现出来的,关于telerik Chart的使用,有几个小点跟大家分享一下. 1:本例子使用的Chart的命名空间为 xmlns:telerik=h ...