首先是实现IDataSeedContributor接口,此类型会自动加到AbpDataOptions的Contributors的列表
public interface IDataSeedContributor { Task SeedAsync(DataSeedContext context); }
IDataSeeder利用Contributors,逐个取出实例化进行执行,其中DataSeedContext是TenantId和自定义的Dictionary<string, object> Properties
扩展一个SeedAsync方法tenantId,如果在多租户的环境,注意要带上
public static Task SeedAsync(this IDataSeeder seeder, Guid? tenantId = null) { return seeder.SeedAsync(new DataSeedContext(tenantId)); }
ABP应用框架增加了两个方法,分别是用户和角色,以及权限
Volo.Abp.PermissionManagement
IPermissionDataSeeder
public virtual Task SeedAsync(DataSeedContext context) { var multiTenancySide = CurrentTenant.GetMultiTenancySide(); var permissionNames = PermissionDefinitionManager .GetPermissions() .Where(p => p.MultiTenancySide.HasFlag(multiTenancySide)) .Select(p => p.Name) .ToArray(); return PermissionDataSeeder.SeedAsync( RolePermissionValueProvider.ProviderName, "admin", permissionNames, context.TenantId ); }
2、IIdentityDataSeeder
Volo.Abp.Identity
public Task SeedAsync(DataSeedContext context) { return _identityDataSeeder.SeedAsync( context["AdminEmail"] as string ?? "admin@abp.io", context["AdminPassword"] as string ?? "1q2w3E*", context.TenantId ); }