feat: 添加租户初始化

This commit is contained in:
橙子
2024-02-08 12:59:21 +08:00
parent 419cadfe1d
commit 64adfcceab
11 changed files with 263 additions and 73 deletions

View File

@@ -110,17 +110,18 @@ namespace Yi.Framework.TenantManagement.Application
[HttpPut("tenant/init/{id}")]
public async Task InitAsync([FromRoute]Guid id)
{
using (CurrentTenant.Change(id))
using (CurrentTenant.Change(id,"test"))
{
CodeFirst(await _repository.GetDbContextAsync());
await CodeFirst(this.LazyServiceProvider);
await _dataSeeder.SeedAsync(id);
}
}
private void CodeFirst(ISqlSugarClient db)
private async Task CodeFirst(IServiceProvider service)
{
var moduleContainer = ServiceProvider.GetRequiredService<IModuleContainer>();
var moduleContainer = service.GetRequiredService<IModuleContainer>();
var db = await _repository.GetDbContextAsync();
//尝试创建数据库
db.DbMaintenance.CreateDatabase();
@@ -131,12 +132,11 @@ namespace Yi.Framework.TenantManagement.Application
types.AddRange(module.Assembly.GetTypes()
.Where(x => x.GetCustomAttribute<IgnoreCodeFirstAttribute>() == null)
.Where(x => x.GetCustomAttribute<SugarTable>() != null)
.Where(x=>x.GetCustomAttribute<MasterTenantAttribute>()==null)
.Where(x => x.GetCustomAttribute<SplitTableAttribute>() is null));
}
if (types.Count > 0)
{
db.CodeFirst.InitTables(types.ToArray());
db.CopyNew().CodeFirst.InitTables(types.ToArray());
}
}