初识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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Celer Network月报 202107

    Celer Network月报 202107Celer技术研发更新 cBridgev1开发完成,主网上线,运行良好 cBridgev2开始研发,新增智能费率与流动性调节功能 Layer2.finance测试网成功运行,完成所有相关问题修复与功能改进 正在进行多项Layer2.finance的策略开发工作 SGN主网技术支持,现已有10个验证节点上线 Celer社区建设及市场活动社区动态更新本月重点事件Layer2.financev1.0版本测…

    2022年5月17日
  • 美元共有几种面值_日元的面值有哪几种

    美元共有几种面值_日元的面值有哪几种美元面值纸币有1美元、2美元、5美元、10美元、20美元、50美元、100美元。以前曾发行过500和1000元面额的大面额钞票,现在已不再流通。美元是美利坚合众国的官方货币。目前流通的美元纸币是自19

    2022年8月4日
  • .net2.0线程使用参数

    .net2.0线程使用参数

    2021年7月25日
  • FileInputStream应用

    FileInputStream应用源码packagecom.io;importjava.io.File;importjava.io.FileInputStream;/***@authoryanyugang*@description*1、FileInputStream读取文件内容*@date2019/10/1310:26*/publicclassFileInputStream…

    2022年5月1日
  • python中替换字符串中字符_Python replace()函数:替换字符串中的某个字符「建议收藏」

    python中替换字符串中字符_Python replace()函数:替换字符串中的某个字符「建议收藏」下面我们将通过一组示例,详细给大家说明下关于用python的替换问题,相信大家结合实例一定非常容易理解,一起来看下吧~基础了解——replace()函数语法:str.replace(old,new[,max])参数:old–将被替换的子字符串。new–新字符串,用于替换old子字符串。max–可选字符串,替换不超过max次。返回值:返回字符串中的old(旧字符串)替…

    2022年6月7日
  • java大数据培训,如何选择适合自己的培训机构开发_大数据培训课程哪个好

    java大数据培训,如何选择适合自己的培训机构开发_大数据培训课程哪个好如何挑选Java大数据培训机构?对于有java的基础的人来说,可以视情况直接跳过java阶段的学习,那么学习时间就可以少一个多月时间,当然前提是基础足够扎实,如果你只是自学了一点java的知识,那么最好还是要从0开始学大数据,选择一家靠谱的Java培训机构。    如何挑选Java大数据培训机构?  想要学好大数据,就要选择好的培训大数据培训机构,那么,如何评判一个培训机构是一个好的培训机构…

    2022年10月21日

发表回复

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

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