• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
netcore_vue
博客园    首页    新随笔    联系   管理    订阅  订阅

abp vnext扩展实体及DTO对象

对于模块的实体属性,都是在模块中定义好的,无法更改模块内部代码。我们可以通过扩展实体来向模块实体增加属性。这些扩展是针对abpvnext底层的一些对象对应的表,比如:IdentityUser,如果是自己创建的就不建议以下这样的更改,直接操作就好了,简单的说,你自己的对象通常不需要对象扩展系统,因为你可以轻松的添加常规属性到你的类中。

Extra Properties

Extra properties是一种存储实体的一些额外数据但不用更改实体的方式. 实体应该实现 IHasExtraProperties 接口. 所有预构建模块定义的聚合根实体都实现了 IHasExtraProperties 接口,所以你可以在这些实体中存储额外的属性.
示例:
//SET AN EXTRA PROPERTY
var user = await _identityUserRepository.GetAsync(userId);
user.SetProperty("Title", "My custom title value!");
await _identityUserRepository.UpdateAsync(user);

//GET AN EXTRA PROPERTY
var user = await _identityUserRepository.GetAsync(userId);
return user.GetProperty<string>("Title");
这种方法开箱即用并且非常简单,你可以使用不同的属性名称(如这里的Title)在同一时间存储多个属性.
对于EF Core额外的属性被格式化成单个 JSON 字符值串存储在数据库中. 对于MongoDB它们做为单独的字段存储.

实体扩展 (EF Core)

如上所述,实体所有的额外属性都作为单个JSON对象存储在数据库表中. 它不适用复杂的场景,特别是在你需要的时候.

  • 使用额外属性创建索引和外键.
  • 使用额外属性编写SQL或LINQ(例如根据属性值搜索).
  • 创建你自己的实体映射到相同的表,但在实体中定义一个额外属性做为 

为了解决上面的问题,用于EF Core的ABP框架实体扩展系统允许你使用上面定义相同的额外属性API,但将所需的属性存储在单独的数据库表字段中.

假设你想要添加 SocialSecurityNumber 到身份模块的 IdentityUser 实体. 你可以使用 ObjectExtensionManager 类:

ObjectExtensionManager.Instance
    .MapEfCoreProperty<IdentityUser, string>(
        "SocialSecurityNumber",
        (entityBuilder, propertyBuilder) =>
        {
            propertyBuilder.HasMaxLength(32);
        }
    );

对应写在解决方案中如下图:

 

 

  扩展DTO,在项目Project.Application.Contracts的ProjectDtoExtensions文件中添加如下代码

ObjectExtensionManager.Instance
    .AddOrUpdateProperty<string>(
        new[] {
            typeof(IdentityUserDto),
            typeof(IdentityUserCreateDto),
            typeof(IdentityUserUpdateDto),
            typeof(ProfileDto),
            typeof(UpdateProfileDto)
        }, "SocialSecurityNumber"
    )
    .AddOrUpdateProperty<Guid>(
        new[] {
            typeof(IdentityUserDto),
            typeof(IdentityUserCreateDto),
            typeof(IdentityUserUpdateDto),
            typeof(ProfileDto),
            typeof(UpdateProfileDto)
        }, "IdentityInfo"
    );

对应的解决方案:

 

然后就是添加迁移更新数据库了:
Add-Migration Added_AppUser_Properties
运行 Update-Database 或着运行DbMigrator项目来更新 

abpnvext版本4.3

应用实例:

 BaseEfCoreEntityExtensionMappings文件中:

引入包:

编写如下代码

 

如下:

public class BaseEfCoreEntityExtensionMappings
    {
        private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();

        public static void Configure()
        {
            BaseServiceModuleExtensionConfigurator.Configure();

            ObjectExtensionManager.Instance
                     .MapEfCoreProperty<IdentityUser, string>(
                         "CodeNum",
                         (entityBuilder, propertyBuilder) =>
                         {
                             
                         }
                     );
        }
    }

BaseServiceApplicationAutoMapperProfile,Mapping一下

 

 DTO扩展

 

 执行数据库迁移

 

生成到现有的表appuser

 

 

 

 

程序中调用这个扩展属性

 

 

 

 

 

posted @ 2021-06-21 14:25  梦想代码-0431  阅读(1973)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3