public bool Delete(Entity entity, string key, IList <PropertyDeleteViewModel> propertiesDeleteOptions) { var deleteOptions = propertiesDeleteOptions.ToDictionary(x => x.PropertyName, x => x.DeleteOption); foreach (var property in entity.Properties.Where(x => x.IsForeignKey && x.IsCollection)) { if (!deleteOptions.ContainsKey(property.ForeignEntity.Name)) { deleteOptions[property.ForeignEntity.Name] = property.DeleteOption; } } var table = new DynamicModel(AdminInitialise.ConnectionString, tableName: entity.TableName, primaryKeyField: entity.Key.ColumnName); var keyObject = GetKeyObject(entity, key); var result = 0; if (deleteOptions.All(x => x.Value == DeleteOption.Nothing)) { result = table.Delete(keyObject); } else { var sql = String.Empty; var recordHierarchy = GetRecordHierarchy(entity); foreach (var subRecord in recordHierarchy.SubRecordsHierarchies) { var deleteOption = DeleteOption.Nothing; if (deleteOptions.ContainsKey(subRecord.Entity.Name)) { deleteOption = deleteOptions[subRecord.Entity.Name]; } if (deleteOption == DeleteOption.SetNull) { sql += GetSetToNullUpdateSql(entity, subRecord) + Environment.NewLine; } else if (deleteOption == DeleteOption.CascadeDelete) { sql += string.Join(Environment.NewLine, GetDeleteRelatedEntityDeleteSql(subRecord).Reverse()) + Environment.NewLine; } } var cmd = table.CreateDeleteCommand(key: keyObject); cmd.CommandText = sql + cmd.CommandText; result = table.Execute(cmd); } if (result < 1) { Error(IlaroAdminResources.EntityNotExist); return(false); } AddEntityChange(entity.Name, key, EntityChangeType.Delete); return(true); }