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

因为用户实体是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); }
        );
});
<
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值