/// <summary> /// Deletes the dependent generic nodes of the corresponding networks. /// </summary> /// <param name="networkIds">The networks whose entities should be deleted.</param> /// <param name="serviceProvider">The application service provider.</param> /// <param name="token">The cancellation token for the task.</param> public static async Task DeleteDependentGenericNodesAsync(IEnumerable<string> networkIds, IServiceProvider serviceProvider, CancellationToken token) { // Define a variable to store the total number of entities. var entityCount = 0; // Use a new scope. using (var scope = serviceProvider.CreateScope()) { // Use a new context instance. using var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); // Get the items in the current batch. entityCount = context.NetworkNodes .Where(item => networkIds.Contains(item.Network.Id)) .Where(item => item.Network.NetworkDatabases.Any(item1 => item1.Database.DatabaseType.Name == "Generic")) .Select(item => item.Node) .Distinct() .Count(); } // Get the total number of batches. var count = Math.Ceiling((double)entityCount / ApplicationDbContext.BatchSize); // Go over each batch. for (int index = 0; index < count; index++) { // Check if the cancellation was requested. if (token.IsCancellationRequested) { // Break. break; } // Define the batch items. var nodes = new List<Node>(); // Use a new scope. using (var scope = serviceProvider.CreateScope()) { // Use a new context instance. using var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); // Get the items in the current batch. nodes = context.NetworkNodes .Where(item => networkIds.Contains(item.Network.Id)) .Where(item => item.Network.NetworkDatabases.Any(item1 => item1.Database.DatabaseType.Name == "Generic")) .Select(item => item.Node) .Distinct() .Take(ApplicationDbContext.BatchSize) .ToList(); // Check if there were no items found. if (nodes == null || !nodes.Any()) { // Continue. continue; } } // Get the IDs of the items. var nodeIds = nodes .Select(item => item.Id); // Delete the related entities. await NodeExtensions.DeleteRelatedEntitiesAsync<DatabaseNodeFieldNode>(nodeIds, serviceProvider, token); await NodeExtensions.DeleteRelatedEntitiesAsync<DatabaseNode>(nodeIds, serviceProvider, token); // Delete the items. await IEnumerableExtensions.DeleteAsync(nodes, serviceProvider, token); } }
/// <summary> /// Deletes the related entities of the corresponding paths. /// </summary> /// <typeparam name="T">The type of the related entities.</typeparam> /// <param name="itemIds">The IDs of the items whose entities should be deleted.</param> /// <param name="serviceProvider">The application service provider.</param> /// <param name="token">The cancellation token for the task.</param> public static async Task DeleteRelatedEntitiesAsync <T>(IEnumerable <string> itemIds, IServiceProvider serviceProvider, CancellationToken token) where T : class, IPathDependent { // Define a variable to store the total number of entities. var entityCount = 0; // Use a new scope. using (var scope = serviceProvider.CreateScope()) { // Use a new context instance. using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); // Get the corresponding database set. var set = context.Set <T>(); // Get the items in the current batch. entityCount = set .Where(item => itemIds.Contains(item.Path.Id)) .Count(); } // Get the total number of batches. var count = Math.Ceiling((double)entityCount / ApplicationDbContext.BatchSize); // Go over each batch. for (int index = 0; index < count; index++) { // Check if the cancellation was requested. if (token.IsCancellationRequested) { // Break. break; } // Define the batch items. var batchItems = new List <T>(); // Use a new scope. using (var scope = serviceProvider.CreateScope()) { // Use a new context instance. using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); // Get the corresponding database set. var set = context.Set <T>(); // Get the items in the current batch. batchItems = set .Where(item => itemIds.Contains(item.Path.Id)) .Take(ApplicationDbContext.BatchSize) .ToList(); // Check if there were no items found. if (batchItems == null || !batchItems.Any()) { // Continue. continue; } } // Delete the items. await IEnumerableExtensions.DeleteAsync(batchItems, serviceProvider, token); } }