private static async Task ReadPosts(SqlConnection connection, MongoDBWrapper wrapper) { while (true) { // Read from Mongo var nextPost = await wrapper.GetNextPost(); if (nextPost == null) { break; } Console.WriteLine($"ReadPosts: Read {nextPost.Id}"); using var transaction = connection.BeginTransaction(); // Write To Sql Server string sql = "DECLARE @newRecord table(newId uniqueidentifier); " + "INSERT INTO Post " + "(Text, WorkoutDate) " + "OUTPUT INSERTED.Id INTO @newRecord " + "VALUES " + "(@text, @workoutDate) " + "SELECT CONVERT(nvarchar(50), newId) FROM @newRecord"; var result = await connection.QueryAsync <string>(sql, new { text = nextPost.Text, workoutDate = nextPost.WorkoutDate }, transaction); // Get all comments for post await ReadComments(transaction, connection, result.Single(), nextPost.Id.ToString(), wrapper); transaction.Commit(); } Console.WriteLine("ReadPosts: End"); }