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. URAL 1684. Jack&#39;s Last Word KMP

    题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后切割 切割的 ...

  2. 回归(regression)的理解(regressor,回归子)

    1. 基本概念 回归(regression)是监督学习(given {(xi,yi)})的一个重要分类.回归用于预测输入变量(自变量,Xi)与输出变量(因变量,Yi) 之间的关系,特定是当输入变量的值 ...

  3. matlab 工具函数 —— logdet(A)

    当参数 A 是正定矩阵(positive definite)时,logdet 利用相关矩阵分解的性质,将比 log(det(A)) 获得更快的效率: function y = logdet(A) tr ...

  4. C++第三方日志库Pantheios

    C++第三方日志库Pantheios Kagula 2012-1-11 简介 在项目实践中发现一个好的日志记录非常重要,你需要借助Logging才能跟踪软件中的错误.所以这里研究下第三方C++库Pan ...

  5. 机器学习:深入理解 LSTM 网络 (一)

    Recurrent Neural Network Long Short Term Memory Networks (LSTMs) 最近获得越来越多的关注,与传统的前向神经网络 (feedforward ...

  6. error C2220: 警告被视为错误 - 没有生成“object”文件

    原文:error C2220: 警告被视为错误 - 没有生成"object"文件 这种错误的原因是:原因是该文件的代码页为英文,而我们系统中的代码页为中文.   解决方案: 1. ...

  7. Windows,linux下编译qt源码(比较简单)

    一.linux下静态编译qt源码 1.取到qt源码并解压到文件夹 2.cd到qt目录下 3.使用configure生成makefile ./configure–prefix /opt/qtstatic ...

  8. 从加载DLL的中获取放置于Resources文件夹中资源字典的几种方法

    原文:从加载DLL的中获取放置于Resources文件夹中资源字典的几种方法 主程序 为 Main_Test.exe 被加载的DLL 为 Load_Test.dll  此DLL 中 有一个 文件夹Re ...

  9. ASP.NET Core SameSite 设置引起 Cookie 在 QQ 浏览器中不起作用

    最近在发布了基于 ASP.NET Core 实现的新版登录页面之后,陆陆续续地接到用户反馈登录时 Antiforgery Token 总是验证失败. 日志中记录的对应错误是 Antiforgery t ...

  10. Expander

    实现折叠列表的效果 <Expander Header="水果列表"> <StackPanel> <RadioButton Content=" ...