doc:添加cicd文档模块
This commit is contained in:
92
Yi.Doc.Md/02.框架功能模块教程/11.种子数据.md
Normal file
92
Yi.Doc.Md/02.框架功能模块教程/11.种子数据.md
Normal file
@@ -0,0 +1,92 @@
|
||||
## 简介
|
||||
种子数据一直都是一个很繁琐的东西,例如在初始化数据的时候,添加默认用户
|
||||
可以通过导入sql的方式进行添加种子数据,也可以通过程序代码中自动初始化数据
|
||||
我们目前提供后者
|
||||
|
||||
## 如何使用
|
||||
一切的根源,来源自:`IDataSeedContributor`
|
||||
直接使用实现`IDataSeedContributor`接口,我们只需要实现 `SeedAsync(DataSeedContext context)`即可
|
||||
|
||||
在实现类上,要将该类加入容器中,推荐通过内置的依赖注入模块
|
||||
|
||||
当然,对于扩展,你可以重写其他的方法
|
||||
|
||||
#### 其他方式使用
|
||||
另外,你可以直接依赖注入,直接使用IDataSeeder SeedAsync方法,重新手动执行种子数据
|
||||
> 默认在程序启动的时候,会根据配置文件选择,是否执行种子数据
|
||||
|
||||
## 完整例子
|
||||
``` cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Volo.Abp.Guids;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.Bbs.SqlSugarCore.DataSeeds
|
||||
{
|
||||
public class ConfigDataSeed : IDataSeedContributor, ITransientDependency
|
||||
{
|
||||
private ISqlSugarRepository<ConfigEntity> _repository;
|
||||
public ConfigDataSeed(ISqlSugarRepository<ConfigEntity> repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
public async Task SeedAsync(DataSeedContext context)
|
||||
{
|
||||
if (!await _repository.IsAnyAsync(x => true))
|
||||
{
|
||||
await _repository.InsertManyAsync(GetSeedData());
|
||||
}
|
||||
}
|
||||
public List<ConfigEntity> GetSeedData()
|
||||
{
|
||||
List<ConfigEntity> entities = new List<ConfigEntity>();
|
||||
ConfigEntity config1 = new ConfigEntity()
|
||||
{
|
||||
ConfigKey = "bbs.site.name",
|
||||
ConfigName = "站点名称",
|
||||
ConfigValue = "意社区"
|
||||
};
|
||||
entities.Add(config1);
|
||||
|
||||
ConfigEntity config2 = new ConfigEntity()
|
||||
{
|
||||
ConfigKey = "bbs.site.author",
|
||||
ConfigName = "站点作者",
|
||||
ConfigValue = "橙子"
|
||||
};
|
||||
entities.Add(config2);
|
||||
|
||||
ConfigEntity config3 = new ConfigEntity()
|
||||
{
|
||||
ConfigKey = "bbs.site.icp",
|
||||
ConfigName = "站点Icp备案",
|
||||
ConfigValue = "赣ICP备20008025号"
|
||||
};
|
||||
entities.Add(config3);
|
||||
|
||||
|
||||
ConfigEntity config4 = new ConfigEntity()
|
||||
{
|
||||
ConfigKey = "bbs.site.bottom",
|
||||
ConfigName = "站点底部信息",
|
||||
ConfigValue = "你好世界"
|
||||
};
|
||||
entities.Add(config4);
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user