/// <summary> /// This will soft delete the single entity. This may delete other dependent /// </summary> /// <param name="softDeleteThisEntity">Mustn't be null</param> /// <param name="callSaveChanges">Defaults to calling SaveChanges. If set to false, then you must call SaveChanges</param> /// <returns>Returns status. If not errors then Result return 1 to say it worked. Zero if error</returns> public async Task <IStatusGeneric <int> > SetSoftDeleteAsync(TInterface softDeleteThisEntity, bool callSaveChanges = true) { if (softDeleteThisEntity == null) { throw new ArgumentNullException(nameof(softDeleteThisEntity)); } var keys = _context.Entry(softDeleteThisEntity).Metadata.GetForeignKeys(); if (!keys.All(x => x.DependentToPrincipal?.IsCollection == true || x.PrincipalToDependent?.IsCollection == true)) { //This it is a one-to-one entity - setting a one-to-one as soft deleted causes problems when you try to create a replacement throw new InvalidOperationException("You cannot soft delete a one-to-one relationship. " + "It causes problems if you try to create a new version."); } var status = new StatusGenericHandler <int>(); if (_config.GetSoftDeleteValue.Compile().Invoke(softDeleteThisEntity)) { return(status.AddError($"This entry is already {_config.TextSoftDeletedPastTense}.")); } _config.SetSoftDeleteValue(softDeleteThisEntity, true); if (callSaveChanges) { await _context.SaveChangesAsync(); } status.Message = $"Successfully {_config.TextSoftDeletedPastTense} this entry"; status.SetResult(1); //one changed return(status); }
/// <summary> /// This will soft delete the single entity. This may delete other dependent /// </summary> /// <param name="softDeleteThisEntity">Mustn't be null</param> /// <param name="callSaveChanges">Defaults to calling SaveChanges. If set to false, then you must call SaveChanges</param> /// <returns>Returns status. If not errors then Result return 1 to say it worked. Zero if error</returns> public async Task <IStatusGeneric <int> > SetSoftDeleteAsync(TInterface softDeleteThisEntity, bool callSaveChanges = true) { if (softDeleteThisEntity == null) { throw new ArgumentNullException(nameof(softDeleteThisEntity)); } _context.ThrowExceptionIfPrincipalOneToOne(softDeleteThisEntity); var status = new StatusGenericHandler <int>(); if (_config.GetSoftDeleteValue.Compile().Invoke(softDeleteThisEntity)) { return(status.AddError($"This entry is already {_config.TextSoftDeletedPastTense}.")); } _config.SetSoftDeleteValue(softDeleteThisEntity, true); if (callSaveChanges) { await _context.SaveChangesAsync(); } status.Message = $"Successfully {_config.TextSoftDeletedPastTense} this entry"; status.SetResult(1); //one changed return(status); }