public async Task SeedInitial() { if (await _dbContext.AccountHierarchies.CountAsync() == 1) { var first = await _dbContext.AccountHierarchies.FirstAsync(); await _dbContext.AccountHierarchies.AddRangeAsync( new AccountHierarchy() { Name = "B", PlacementPreference = 2, ParentId = first.Id, UplinkId = first.Id, LevelPath = "/1/" }, new AccountHierarchy() { Name = "C", PlacementPreference = 3, ParentId = first.Id, UplinkId = first.Id, LevelPath = "/2/" }, new AccountHierarchy() { Name = "D", PlacementPreference = 3, ParentId = first.Id + 1, UplinkId = first.Id, LevelPath = "/1/1/" }, new AccountHierarchy() { Name = "H", PlacementPreference = 1, ParentId = first.Id + 2, UplinkId = first.Id, LevelPath = "/2/1/" }, new AccountHierarchy() { Name = "K", PlacementPreference = 3, ParentId = first.Id + 2, UplinkId = first.Id, LevelPath = "/2/2/" }, new AccountHierarchy() { Name = "F", PlacementPreference = 2, ParentId = first.Id + 1, UplinkId = first.Id, LevelPath = "/1/2/" }, new AccountHierarchy() { Name = "G", PlacementPreference = 3, ParentId = first.Id + 3, UplinkId = first.Id, LevelPath = "/1/1/1/" }, new AccountHierarchy() { Name = "V", PlacementPreference = 3, ParentId = first.Id + 3, UplinkId = first.Id, LevelPath = "/1/1/2/" }, new AccountHierarchy() { Name = "L", PlacementPreference = 3, ParentId = first.Id + 5, UplinkId = first.Id, LevelPath = "/2/2/1/" } ); } await _dbContext.SaveChangesAsync(); }
private async Task <int> SeedBatch(int numOfEntities, string namePrefix) { if (numOfEntities < 1 || string.IsNullOrEmpty(namePrefix)) { throw new ArgumentException("Invalid number or prefix"); } var accounts = new List <Account>(); var countOfchildrenDict = new Dictionary <long, int>(); var allParentIds = await _dbContext.Accounts.Where(a => a.ParentId != null).Select(a => a.ParentId).ToListAsync(); foreach (long id in allParentIds) { if (!countOfchildrenDict.ContainsKey(id)) { countOfchildrenDict[id] = 0; } countOfchildrenDict[id] += 1; } var existingIds = await _dbContext.Accounts .OrderBy(a => a.Id) .Select(a => a.Id) .ToListAsync(); var random = new Random(); for (int i = 0; i < numOfEntities; i++) { //long? parentId = ChooseParent(existingIds, countOfchildrenDict); long uplinkId = existingIds[random.Next(0, existingIds.Count)]; accounts.Add(new Account { Name = namePrefix + (i + 1), PlacementPreference = random.Next(1, 4), //Leg = random.Next(1,3), UplinkId = uplinkId, //ParentId = parentId }); // if(parentId != null) // { // if(!countOfchildrenDict.ContainsKey((long)parentId)) // { // countOfchildrenDict[(long)parentId] = 0; // } // countOfchildrenDict[(long)parentId] += 1; // } } if (accounts.Count > 0) { foreach (var item in accounts) { await this.AddNode(item); } //await _dbContext.AddRangeAsync(accounts); await _dbContext.SaveChangesAsync(); } return(accounts.Count); }