public void Add(Webhook webhook) { var webhookInput = webhook.ToInput(); var insertWebhookSql = $"insert into ObsoleteWebhooks values (@{nameof(WebhookInput.Id)}, @{nameof(WebhookInput.TenantId)}, @{nameof(WebhookInput.PostbackUrl)}, @{nameof(WebhookInput.Secret)})"; var deleteSubscriptionsSql = $"delete from Subscriptions where WebhookId = @{nameof(WebhookInput.Id)}"; var subscriptionsInput = webhookInput.EventTypeIds.Select(eventTypeId => new SubscriptionInput(webhookInput.Id, eventTypeId)); var insertSubscriptionsSql = $"insert into Subscriptions values (@{nameof(SubscriptionInput.WebhookId)}, @{nameof(SubscriptionInput.EventTypeId)})"; using (var connection = new SqlConnection(connectionString)) { connection.Open(); using (var transaction = connection.BeginTransaction()) { connection.Execute(insertWebhookSql, webhookInput, transaction); connection.Execute(deleteSubscriptionsSql, webhookInput, transaction); connection.Execute(insertSubscriptionsSql, subscriptionsInput, transaction); // Surely, there must be a more efficient way of updating the subscriptions? transaction.Commit(); } } }