初识ABP vNext(5):ABP扩展实体

初识ABP vNext(5):ABP扩展实体

Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章。

前言

上一篇实现了前端vue部分的用户登录和菜单权限控制,但是有一些问题需要解决,比如用户头像、用户介绍字段目前还没有,下面就来完善一下。

开始

因为用户实体是ABP模板自动生成的,其中的属性都预先定义好了,但是ABP是允许我们扩展模块实体的,我们可以通过扩展用户实体来增加用户头像和用户介绍字段。

扩展实体

ABP支持多种扩展实体的方式:

  1. 将所有扩展属性以json格式存储在同一个数据库字段中
  2. 将每个扩展属性存储在独立的数据库字段中
  3. 创建一个新的实体类映射到原有实体的同一个数据库表中
  4. 创建一个新的实体类映射到独立的数据库表中

这里选择第2种方式就好,它们的具体区别请见官网:扩展实体

src\Xhznl.HelloAbp.Domain\Users\AppUser.cs:

/// <summary>
/// 头像
/// </summary>
public string Avatar { get; set; }

/// <summary>
/// 个人介绍
/// </summary>
public string Introduction { get; set; }

src\Xhznl.HelloAbp.EntityFrameworkCore\EntityFrameworkCore\HelloAbpDbContext.cs:

builder.Entity<AppUser>(b =>
{
    。。。。。。
        
    b.Property(x => x.Avatar).IsRequired(false).HasMaxLength(AppUserConsts.MaxAvatarLength).HasColumnName(nameof(AppUser.Avatar));
    b.Property(x => x.Introduction).IsRequired(false).HasMaxLength(AppUserConsts.MaxIntroductionLength).HasColumnName(nameof(AppUser.Introduction));
});

src\Xhznl.HelloAbp.EntityFrameworkCore\EntityFrameworkCore\HelloAbpEfCoreEntityExtensionMappings.cs:

OneTimeRunner.Run(() =>
{
    ObjectExtensionManager.Instance
        .MapEfCoreProperty<IdentityUser, string>(
            nameof(AppUser.Avatar),
            b => { b.HasMaxLength(AppUserConsts.MaxAvatarLength); }
        )
        .MapEfCoreProperty<IdentityUser, string>(
            nameof(AppUser.Introduction),
            b => { b.HasMaxLength(AppUserConsts.MaxIntroductionLength); }
        );
});

src\Xhznl.HelloAbp.Application.Contracts\HelloAbpDtoExtensions.cs:

OneTimeRunner.Run(() =>
{
    ObjectExtensionManager.Instance
        .AddOrUpdateProperty<string>(
            new[]
            {
                typeof(IdentityUserDto),
                typeof(IdentityUserCreateDto),
                typeof(IdentityUserUpdateDto),
                typeof(ProfileDto),
                typeof(UpdateProfileDto)
            },
            "Avatar"
        )
        .AddOrUpdateProperty<string>(
            new[]
            {
                typeof(IdentityUserDto),
                typeof(IdentityUserCreateDto),
                typeof(IdentityUserUpdateDto),
                typeof(ProfileDto),
                typeof(UpdateProfileDto)
            },
            "Introduction"
        );
});

注意最后一步,Dto也需要添加扩展属性,不然就算你实体中已经有了新字段,但接口依然获取不到。

然后就是添加迁移更新数据库了:

Add-Migration Added_AppUser_Properties

Update-Database 也可以不用update,运行DbMigrator项目来更新

<span>初识ABP vNext(5):ABP扩展实体</span>

查看数据库,AppUsers表已经生成这2个字段了:

<span>初识ABP vNext(5):ABP扩展实体</span>

目前还没做设置界面,我先手动给2个初始值:

<span>初识ABP vNext(5):ABP扩展实体</span>

再次请求/api/identity/my-profile接口,已经返回了这2个扩展字段:

<span>初识ABP vNext(5):ABP扩展实体</span>

修改一下前端部分:

src\store\modules\user.js:

// get user info
getInfo({ commit }) {
  return new Promise((resolve, reject) => {
    getInfo()
      .then(response => {
        const data = response;
        if (!data) {
          reject("Verification failed, please Login again.");
        }
        const { name, extraProperties } = data;
        commit("SET_NAME", name);
        commit("SET_AVATAR", extraProperties.Avatar);
        commit("SET_INTRODUCTION", extraProperties.Introduction);
        resolve(data);
      })
      .catch(error => {
        reject(error);
      });
  });
},

刷新界面,右上角的用户头像就回来了:

<span>初识ABP vNext(5):ABP扩展实体</span>

路由整理

删除掉vue-element-admin多余的路由,并添加ABP模板自带的身份认证管理和租户管理。

src\router\index.js:

/* Router Modules */
import identityRouter from "./modules/identity";
import tenantRouter from "./modules/tenant";

export const asyncRoutes = [
  /** when your routing map is too long, you can split it into small modules **/
  identityRouter,
  tenantRouter,
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true }
];

src\router\modules\identity.js:

/** When your routing table is too long, you can split it into small modules **/

import Layout from "@/layout";

const identityRouter = {
  path: "/identity",
  component: Layout,
  redirect: "noRedirect",
  name: "Identity",
  meta: {
    title: "identity",
    icon: "user"
  },
  children: [
    {
      path: "roles",
      component: () => import("@/views/identity/roles"),
      name: "Roles",
      meta: { title: "roles", policy: "AbpIdentity.Roles" }
    },
    {
      path: "users",
      component: () => import("@/views/identity/users"),
      name: "Users",
      meta: { title: "users", policy: "AbpIdentity.Users" }
    }
  ]
};
export default identityRouter;

src\router\modules\tenant.js:

/** When your routing table is too long, you can split it into small modules **/

import Layout from "@/layout";

const tenantRouter = {
  path: "/tenant",
  component: Layout,
  redirect: "/tenant/tenants",
  alwaysShow: true,
  name: "Tenant",
  meta: {
    title: "tenant",
    icon: "tree"
  },
  children: [
    {
      path: "tenants",
      component: () => import("@/views/tenant/tenants"),
      name: "Tenants",
      meta: { title: "tenants", policy: "AbpTenantManagement.Tenants" }
    }
  ]
};
export default tenantRouter;

运行效果:

<span>初识ABP vNext(5):ABP扩展实体</span>

对应ABP模板界面:

<span>初识ABP vNext(5):ABP扩展实体</span>

最后

本篇介绍了ABP扩展实体的基本使用,并且整理了前端部分的系统菜单,但是菜单的文字显示不对。下一篇将介绍ABP本地化,让系统文字支持多国语言。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2642.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ajax的responseText是什么东西

    你向ajax后台的程序发送xmlhttp请求的时候,后台程序接到请求会进行处理,处理结束后,可以返回一串数据给前台,这个就是responseText. 一般在后台程序C#中是Response.Write(“字符串”)php中使用的是echo…就是一个输出字符串而已

    2022年4月5日
    47
  • 良心推荐JVM性能调优工具

    良心推荐JVM性能调优工具良心推荐JVM性能调优工具该工具源于几年前关注的一个阿里大佬专做jvm性能调优,现在已经创业开发出社区版的在线性能调优!我觉得很香就推荐给大家!工具地址https://console.perfma.com/下面简单介绍社区版三个产品XXFox-Java虚拟机参数分析参数查询:查询指定JVM参数的官方定义、使用方法、默认值、专家建议参数检查:对JVM参数的用法,赋值及组合进行正确性与合理性检查参数变迁:给出JVM参数在不同配置环境下推荐取值或替代参数参数优化:全面诊断JVM参数,给出综

    2022年5月18日
    38
  • idea设置全局搜索快捷键

    idea设置全局搜索快捷键idea默认的全局搜索二、修改为Alt+F

    2022年6月22日
    302
  • ubuntu系统使用Anaconda安装tensorflow-gpu环境

    ubuntu系统使用Anaconda安装tensorflow-gpu环境

    2021年4月9日
    157
  • MyBatis工作原理

    在学习MyBatis程序之前,需要了解一下MyBatis工作原理,以便于理解程序。MyBatis的工作原理如下图1)读取MyBatis配置文件:mybatis-config.xml为MyBatis的全局配置文件,配置了MyBatis的运行环境等信息,例如数据库连接信息。2)加载映射文件。映射文件即SQL映射文件,该文件中配置了操作数据库的SQL语句,需要在M…

    2022年4月6日
    41
  • Lotus Notes视图索引的机制

    Lotus Notes视图索引的机制
    内容提要:
    本文对视图的索引机制进行说明。包括:术语、索引的机制、视图索引的选项说明。
    说明
    视图索引的机制
    第1章概述
    本文档主要是对视图的索引机制进行说明。包括:术语、索引的机制、视图索引的选项说明。
    文档中用到的术语:
    更新(Refresh):按F9可以刷新视图的索引。Refresh读视图的索引并刷新用户的屏幕。它不会重建视图的索引。
    重建(Rebuild):按Shift+F9可以重建视图的索引。重建视

    2022年7月22日
    20

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号