当前位置: 首页 > news >正文

ABP VNext种子数据按顺序插入

ABP VNext种子数据按顺序插入

  • 1.Domain层
    • 1.1 新加Author和Book实体
    • 1.2 CustomDataSeedDbMigrationService新加方法
    • 1.3新加ISowSeed接口
    • 1.4 作者和图书种子数据逻辑
    • 1.5 新加CustomDataSeedDataSeederContributor
  • 2.EntityFrameworkCore
    • 2.1 CustomDataSeedDbContext
    • 2.2 生成迁移脚本
    • 2.3 应用到数据库
  • 3.源码

1.Domain层

1.1 新加Author和Book实体

 public class Author : FullAuditedAggregateRoot<Guid>{public string Name { get; private set; }public DateTime BirthDate { get; set; }public string ShortBio { get; set; }private Author(){/* This constructor is for deserialization / ORM purpose */}internal Author(Guid id,[NotNull] string name,DateTime birthDate,[CanBeNull] string shortBio = null): base(id){SetName(name);BirthDate = birthDate;ShortBio = shortBio;}internal Author ChangeName([NotNull] string name){SetName(name);return this;}private void SetName([NotNull] string name){Name = Check.NotNullOrWhiteSpace(name,nameof(name),maxLength: 100);}}
public class Book : AuditedAggregateRoot<Guid>, IMultiTenant{public string Name { get; set; }public BookType Type { get; set; }public DateTime PublishDate { get; set; }public float Price { get; set; }public Guid AuthorId { get; set; }public Guid? TenantId { get; set; }}

1.2 CustomDataSeedDbMigrationService新加方法

CustomDataSeedDbMigrationService中的MigrateAsync方法中添加自定义的种子数据插入方法:

    private async Task CustomSeedDataAsync(bool isUseCustomRank = true){var dataSeedContext = new DataSeedContext().WithProperty(IdentityDataSeedContributor.AdminEmailPropertyName, "admin@ycims.com").WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName, "ycqwe123").WithProperty("IsUseCustomRank", isUseCustomRank);await _dataSeeder.SeedAsync(dataSeedContext);}

MigrateAsync完整代码:

public async Task MigrateAsync(){var initialMigrationAdded = AddInitialMigrationIfNotExist();if (initialMigrationAdded){return;}Logger.LogInformation("Started database migrations...");await MigrateDatabaseSchemaAsync();await SeedDataAsync();//自定义顺序插入种子数据await CustomSeedDataAsync();Logger.LogInformation($"Successfully completed host database migrations.");var tenants = await _tenantRepository.GetListAsync(includeDetails: true);var migratedDatabaseSchemas = new HashSet<string>();foreach (var tenant in tenants){using (_currentTenant.Change(tenant.Id)){if (tenant.ConnectionStrings.Any()){var tenantConnectionStrings = tenant.ConnectionStrings.Select(x => x.Value).ToList();if (!migratedDatabaseSchemas.IsSupersetOf(tenantConnectionStrings)){await MigrateDatabaseSchemaAsync(tenant);migratedDatabaseSchemas.AddIfNotContains(tenantConnectionStrings);}}await SeedDataAsync(tenant);}Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations.");}Logger.LogInformation("Successfully completed all database migrations.");Logger.LogInformation("You can safely end this process...");}

1.3新加ISowSeed接口

 public interface ISowSeed{/// <summary>/// 种子执行顺序(从小到大执行)/// </summary>int Sort { get; set; }Task ExecAsync(DataSeedContext context);}

1.4 作者和图书种子数据逻辑

这里作者的数据顺序为1,图书为2

 public class AuthorSowSeed : ISowSeed, ITransientDependency{private readonly IRepository<Author, Guid> _authorRepository;private readonly IGuidGenerator _guidGenerator;public AuthorSowSeed(IRepository<Author, Guid> authorRepository, IGuidGenerator guidGenerator){_authorRepository = authorRepository;_guidGenerator = guidGenerator;}public int Sort { get; set; } = 1;public async Task ExecAsync(DataSeedContext context){await this.MockData();}private async ValueTask MockData(){var author1 = await _authorRepository.FindAsync(s => s.Name == "George Orwell");if (author1 == null){await _authorRepository.InsertAsync(new Author(_guidGenerator.Create(), "George Orwell", new DateTime(1903, 06, 25), "Orwell produced literary criticism and poetry, fiction and polemical journalism; and is best known for the allegorical novella Animal Farm (1945) and the dystopian novel Nineteen Eighty-Four (1949)."), autoSave: true);}var author2 = await _authorRepository.FindAsync(s => s.Name == "Douglas Adams");if (author2 == null){var douglas = await _authorRepository.InsertAsync(new Author(_guidGenerator.Create(), "Douglas Adams", new DateTime(1952, 03, 11), "Douglas Adams was an English author, screenwriter, essayist, humorist, satirist and dramatist. Adams was an advocate for environmentalism and conservation, a lover of fast cars, technological innovation and the Apple Macintosh, and a self-proclaimed 'radical atheist'."), autoSave: true);}}}public class BookSowSeed : ISowSeed, ITransientDependency{private readonly IRepository<Book, Guid> _bookRepository;private readonly IRepository<Author, Guid> _authorRepository;public BookSowSeed(IRepository<Book, Guid> bookRepository, IRepository<Author, Guid> authorRepository){_bookRepository = bookRepository;_authorRepository = authorRepository;}public int Sort { get; set; } = 2;public async Task ExecAsync(DataSeedContext context){await this.MockData();}private async ValueTask MockData(){var book1 = await _bookRepository.FindAsync(s => s.Name == "1984");if (book1 == null){await _bookRepository.InsertAsync(new Book{AuthorId = (await _authorRepository.FindAsync(s => s.Name == "George Orwell")).Id, // SET THE AUTHORName = "1984",Type = BookType.Dystopia,PublishDate = new DateTime(1949, 6, 8),Price = 19.84f},autoSave: true);}var book2 = await _bookRepository.FindAsync(s => s.Name == "The Hitchhiker's Guide to the Galaxy");if (book2 == null){await _bookRepository.InsertAsync(new Book{AuthorId = (await _authorRepository.FindAsync(s => s.Name == "Douglas Adams")).Id, // SET THE AUTHORName = "The Hitchhiker's Guide to the Galaxy",Type = BookType.ScienceFiction,PublishDate = new DateTime(1995, 9, 27),Price = 42.0f},autoSave: true);}}}

1.5 新加CustomDataSeedDataSeederContributor

    internal class CustomDataSeedDataSeederContributor : IDataSeedContributor, ITransientDependency{private readonly IEnumerable<ISowSeed> _dataSowSeeds;public CustomDataSeedDataSeederContributor(IEnumerable<ISowSeed> dataSowSeeds){this._dataSowSeeds = dataSowSeeds;}public async Task SeedAsync(DataSeedContext context){if (!context.Properties.ContainsKey("IsUseCustomRank")){return;}var items = _dataSowSeeds.OrderBy(p => p.Sort);foreach (var item in items){await item.ExecAsync(context);}}}

2.EntityFrameworkCore

2.1 CustomDataSeedDbContext

    public DbSet<Book> Books { get; set; }public DbSet<Author> Authors { get; set; }builder.Entity<Book>(b =>{b.ToTable(CustomDataSeedConsts.DbTablePrefix + "Books",CustomDataSeedConsts.DbSchema);b.ConfigureByConvention(); //auto configure for the base class propsb.Property(x => x.Name).IsRequired().HasMaxLength(128);b.HasOne<Author>().WithMany().HasForeignKey(x => x.AuthorId).IsRequired();});builder.Entity<Author>(b =>{b.ToTable(CustomDataSeedConsts.DbTablePrefix + "Authors",CustomDataSeedConsts.DbSchema);b.ConfigureByConvention();b.Property(x => x.Name).IsRequired().HasMaxLength(100);b.HasIndex(x => x.Name);});

2.2 生成迁移脚本

dotnet ef migrations add AddBooks

2.3 应用到数据库

应用到数据库之后,启动CustomDataSeed.DbMigrator项目可以打断点查看种子数据生成顺序

dotnet ef database update

3.源码

源代码

相关文章:

ABP VNext种子数据按顺序插入

ABP VNext种子数据按顺序插入 1.Domain层1.1 新加Author和Book实体1.2 CustomDataSeedDbMigrationService新加方法1.3新加ISowSeed接口1.4 作者和图书种子数据逻辑1.5 新加CustomDataSeedDataSeederContributor 2.EntityFrameworkCore2.1 CustomDataSeedDbContext2.2 生成迁移脚…...

Verilog | FIFO简单实现

FIFO( First Input First Output)简单说就是指先进先出&#xff0c;也是缓存机制的一种&#xff0c;下面是我总结的 FIFO 的三大用途&#xff1a; 1)提高传输效率&#xff0c;增加 DDR 带宽的利用率。比如我们有 4 路视频数据缓存到 DDR 中去&#xff0c;比较笨的方法是&#x…...

设计模式应用场景

设计模式简介 工厂模式&#xff08;Factory Pattern&#xff09;&#xff1a;使用工厂方法创建对象&#xff0c;而不是使用new关键字直接实例化对象。 抽象工厂模式&#xff08;Abstract Factory Pattern&#xff09;&#xff1a;提供一个创建一系列相关对象的接口&#xff0c;…...

还在老一套?STM32使用新KEIL5的IDE,全新开发模式RTE介绍及使用

Keil新版本出来了&#xff0c;推出了一种全新开发模式RTE框架( Run-Time Environment)&#xff0c;更好用了。然而网上的教程资料竟还都是把Keil5当成Keil4来用&#xff0c;直接不使用这个功能。当前正点原子或野火的教程提供的例程虽有提到Keil5&#xff0c;但也是基本上当Kei…...

Java时间类(十一) -- Date类工具类 -- Java获取当天、本周、本月、本年 开始及结束时间

目录 1. 今天的日期如下: 2. DateUtils工具类的源代码: 3. 测试类 1. 今天的日期如下:...

Alma Linux 9.2、Rocky Linux 9.2现在是RHEL 9.2的替代品

随着Red Hat Enterprise Linux (RHEL) 9.2的发布&#xff0c;Alma Linux 9.2和Rocky Linux 9.2成为了RHEL 9.2的备选替代品。这两个Linux发行版旨在提供与RHEL兼容的功能和稳定性&#xff0c;以满足那些需要企业级操作系统的用户需求。本文将详细介绍Alma Linux 9.2和Rocky Lin…...

推荐5款提高生活和工作效率的好帮手

在这个数字化时代,软件工具已经深深地影响和改变了我们的生活和工作。有着各种各样的软件工具,它们都可以在特定的领域内让我们变得更加高效,完成复杂的任务。选择一款适合你的软件工具,不但可以极大地释放生产力,也可以让生活变得更加便捷。 1.桌面图标管理工具——TileIconi…...

美团小组长薪资被应届生员工倒挂7K,不把老员工当人?

一位美团的小管理爆出&#xff0c;无意中看到了整个部门薪资&#xff0c;本以为自己算比较高的&#xff0c;但看完之后整个人都傻眼了。小组长的职位月薪28K&#xff0c;而手下组员却是35K&#xff0c;当天晚上抽了一包烟也没想明白是为什么。 楼主表示&#xff0c;自己是美团的…...

【Java多线程案例】使用阻塞队列实现生产者消费者模型

前言 本篇文章讲解多线程案例之阻塞队列。主要讲解阻塞队列的特性、实际开发中常用的到的生产者消费者模型&#xff0c;以及生产者消费者模型解耦合、削峰填谷的好处。并且使用 Java 多线程模拟实现一个生产者消费者模型、阻塞队列版的生产者消费者模型。 文章从什么是阻塞队列…...

Spark 3:Spark Core RDD持久化

RDD 的数据是过程数据 RDD 的缓存 # coding:utf8 import timefrom pyspark import SparkConf, SparkContext from pyspark.storagelevel import StorageLevelif __name__ __main__:conf SparkConf().setAppName("test").setMaster("local[*]")sc SparkC…...

字节跳动五面都过了,结果被刷了,问了hr原因竟说是...

摘要 说在前面&#xff0c;面试时最好不要虚报工资。本来字节跳动是很想去的&#xff0c;几轮面试也通过了&#xff0c;最后没offer&#xff0c;自己只想到几个原因&#xff1a;1、虚报工资&#xff0c;比实际高30%&#xff1b;2、有更好的人选&#xff0c;这个可能性不大&…...

Python日期带时区转换工具类总结

文章目录 1.背景2. 遇到的坑3. 一些小案例3.1 当前日期、日期时间、UTC日期时间3.2 昨天、昨天UTC日期、昨天现在这个时间点的时间戳3.3 日期转时间戳3.4 时间戳转日期3.5 日期加减、小时的加减 4. 总结5. 完整的编码 1.背景 最近项目是国际项目&#xff0c;所以需要经常需要用…...

视频会议产品对比分析

内网视频会议系统如何选择&#xff1f;有很多单位为了保密&#xff0c;只能使用内部网络&#xff0c;无法连接互联网&#xff0c;那些SaaS视频会议就无法使用。在内网的优秀视频会议也有很多可供选择&#xff0c;以下是几个常用的&#xff1a; 1. 宝利通&#xff1a;它支持多种…...

每日一练 | 华为认证真题练习Day47

1、某台路由器输出信息如下&#xff0c;下列说法错误的是&#xff1f;&#xff08;多选&#xff09; A. 本路由器开启了区域认证 B. 本设备出现故障&#xff0c;配置的Router Id和实际生效的Router ID不一致 C. 本设备生效的Router Id为10.0.12.1 D. 本设备生效的Router Id为…...

ChatIE(LLM大模型用于信息抽取)

Zero-Shot Information Extraction via Chatting with ChatGPT paper&#xff1a;https://arxiv.org/abs/2302.10205 利用ChatGPT实现零样本信息抽取&#xff08;Information Extraction&#xff0c;IE&#xff09;&#xff0c;看到零样本就能大概明白这篇文章将以ChatGPT作为…...

提升企业管理效率的利器——ADManager Plus

在当今信息时代&#xff0c;企业的规模和复杂性不断增长&#xff0c;管理各个方面变得愈发具有挑战性。而在企业管理中&#xff0c;活跃目录&#xff08;Active Directory&#xff09;起着至关重要的作用。它是一种用于组织内部的用户、计算机、组和其他对象进行集中管理的目录…...

《入侵的艺术》读书心得:第六章:渗透测试中的智慧与愚昧

第六章&#xff1a;渗透测试中的智慧与愚昧 这些想法是愚昧的 1.任何期待渗透测试结果是“毫无破绽”、“无懈可击”…都是极其愚昧的&#xff1a; 第一层含义&#xff1a;测试的不可穷尽性原理&#xff08;同软件测试&#xff09; 第二层含义&#xff1a;作为优秀甚至只是合…...

SAP-MM-采购申请-价值特性

采购申请审批在维护价值特性时要注意是抬头价值还是行价值&#xff0c;要确定选择哪个&#xff0c;配置时对应配置。 1、创建价值特性CT04 字段名称&#xff1a;CEBAN-GSWRT&#xff0c;和CEBAN-GFWRT 抬头总价值&#xff1a;CEBAN-GFWRT&#xff1b;如果选择的是抬头审批&am…...

设计模式 - 代理模式

基本介绍: 代理模式&#xff1a;为一个对象提供一个替身&#xff0c;以控制对这个对象的访问。即通过代理 对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的 功能操作,即扩展目标对象的功能。被代理的对象可以是远程对象、创建开销大的对象或需要安全控…...

IOC初始化 IOC启动阶段 (Spring容器的启动流程)

[toc](IOC初始化 IOC启动阶段 (Spring容器的启动流程)) IOC初始化 IOC启动阶段 (Spring容器的启动流程) Resource定位过程&#xff1a;这个过程是指定位BeanDefinition的资源&#xff0c;也就是配置文件&#xff08;如xml&#xff09;的位置&#xff0c;并将其封装成Resource对…...

Redis相关知识总结(缓存雪崩,缓存穿透,缓存击穿,Redis实现分布式锁,如何保持数据库和缓存一致)

文章目录 1.什么是Redis&#xff1f;2.为什么要使用redis作为mysql的缓存&#xff1f;3.什么是缓存雪崩、缓存穿透、缓存击穿&#xff1f;3.1缓存雪崩3.1.1 大量缓存同时过期3.1.2 Redis宕机 3.2 缓存击穿3.3 缓存穿透3.4 总结 4. 数据库和缓存如何保持一致性5. Redis实现分布式…...

如何在看板中有效管理突发紧急任务

在看板中有效管理突发紧急任务需要&#xff1a;设立专门的紧急任务通道、重新调整任务优先级、保持适度的WIP&#xff08;Work-in-Progress&#xff09;弹性、优化任务处理流程、提高团队应对突发情况的敏捷性。其中&#xff0c;设立专门的紧急任务通道尤为重要&#xff0c;这能…...

Python如何给视频添加音频和字幕

在Python中&#xff0c;给视频添加音频和字幕可以使用电影文件处理库MoviePy和字幕处理库Subtitles。下面将详细介绍如何使用这些库来实现视频的音频和字幕添加&#xff0c;包括必要的代码示例和详细解释。 环境准备 在开始之前&#xff0c;需要安装以下Python库&#xff1a;…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

全志A40i android7.1 调试信息打印串口由uart0改为uart3

一&#xff0c;概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本&#xff1a;2014.07&#xff1b; Kernel版本&#xff1a;Linux-3.10&#xff1b; 二&#xff0c;Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01)&#xff0c;并让boo…...

全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比

目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec&#xff1f; IPsec VPN 5.1 IPsec传输模式&#xff08;Transport Mode&#xff09; 5.2 IPsec隧道模式&#xff08;Tunne…...

mysql已经安装,但是通过rpm -q 没有找mysql相关的已安装包

文章目录 现象&#xff1a;mysql已经安装&#xff0c;但是通过rpm -q 没有找mysql相关的已安装包遇到 rpm 命令找不到已经安装的 MySQL 包时&#xff0c;可能是因为以下几个原因&#xff1a;1.MySQL 不是通过 RPM 包安装的2.RPM 数据库损坏3.使用了不同的包名或路径4.使用其他包…...

Element Plus 表单(el-form)中关于正整数输入的校验规则

目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入&#xff08;联动&#xff09;2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...

代码随想录刷题day30

1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币&#xff0c;另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额&#xff0c;返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...

使用LangGraph和LangSmith构建多智能体人工智能系统

现在&#xff0c;通过组合几个较小的子智能体来创建一个强大的人工智能智能体正成为一种趋势。但这也带来了一些挑战&#xff0c;比如减少幻觉、管理对话流程、在测试期间留意智能体的工作方式、允许人工介入以及评估其性能。你需要进行大量的反复试验。 在这篇博客〔原作者&a…...