From 3d77fdcacab65aa73bf42002fbd9a5b528d1ecfb Mon Sep 17 00:00:00 2001 From: Oluwatobi Awe Date: Wed, 3 Jul 2024 13:44:12 +0100 Subject: [PATCH] TD-3948 refactored the SPBlockCollectionDelete call not to use "the using statement" during initialization which disposes the DbContext immediately after the asynchronous call starts. --- .../Resources/BlockCollectionRepository.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/WebAPI/LearningHub.Nhs.Repository/Resources/BlockCollectionRepository.cs b/WebAPI/LearningHub.Nhs.Repository/Resources/BlockCollectionRepository.cs index 5954de7c5..554fdde25 100644 --- a/WebAPI/LearningHub.Nhs.Repository/Resources/BlockCollectionRepository.cs +++ b/WebAPI/LearningHub.Nhs.Repository/Resources/BlockCollectionRepository.cs @@ -83,10 +83,22 @@ public async Task DeleteBlockCollection(int userId, int blockCollectionId) foreach (var id in collectionIds) { - using (var lhContext = new LearningHubDbContext(this.DbContext.Options)) + _ = Task.Run(async () => { - _ = lhContext.Database.ExecuteSqlRawAsync("resources.BlockCollectionDelete @p0", new SqlParameter("@p0", SqlDbType.Int) { Value = id }); - } + var lhContext = new LearningHubDbContext(this.DbContext.Options); + try + { + await lhContext.Database.ExecuteSqlRawAsync("resources.BlockCollectionDelete @p0", new SqlParameter("@p0", SqlDbType.Int) { Value = id }); + } + catch (Exception ex) + { + throw new Exception(ex.Message); + } + finally + { + await lhContext.DisposeAsync(); + } + }); } }