public async Task <int> InsertAsync(IDbConnection connection, IDbTransaction transaction, int?commandTimeout, String tableName, string columnList, string parameterList, IEnumerable <PropertyInfo> keyProperties, object entityToInsert)
    {
        var cmd   = $"INSERT INTO {tableName} ({columnList}) values ({parameterList}); SELECT SCOPE_IDENTITY() id";
        var multi = await connection.QueryMultipleAsync(cmd, entityToInsert, transaction, commandTimeout);

        var first = multi.Read().FirstOrDefault();

        if (first == null || first.id == null)
        {
            return(0);
        }

        var id = (int)first.id;
        var pi = keyProperties as PropertyInfo[] ?? keyProperties.ToArray();

        if (!pi.Any())
        {
            return(id);
        }

        var idp = pi.First();

        idp.SetValue(entityToInsert, Convert.ChangeType(id, idp.PropertyType), null);

        return(id);
    }
            public static async Task<List<AvailabilityGroupReplicaInfo>> PopulateFromConnectionAsync(DbConnection conn, SQLNode node)
            {
                List<AvailabilityGroupReplicaInfo> groups;
                List<DatabaseReplicaState> databases;

                var sql = node.GetFetchSQL<AvailabilityGroupReplicaInfo>() + "\n\n" + node.GetFetchSQL<DatabaseReplicaState>();
                using (var multi = await conn.QueryMultipleAsync(sql))
                {
                    groups = (await multi.ReadAsync<AvailabilityGroupReplicaInfo>()).AsList();
                    databases = (await multi.ReadAsync<DatabaseReplicaState>()).AsList();
                }

                Func<string, string, PerfCounterRecord> getCounter = (cn, n) => node.GetPerfCounter("Availability Replica", cn, n);
                groups.ForEach(r =>
                {
                    r.Databases = databases.Where(gdb => r.GroupId == gdb.GroupId && r.ReplicaId == gdb.ReplicaId).ToList();

                    var instanceName = r.AvailabilityGroupName + ":" + r.ReplicaServerName;
                    var sc = getCounter("Bytes Sent to Transport/sec", instanceName);
                    if (sc != null)
                    {
                        r.BytesSentPerSecond = sc.CalculatedValue;
                        r.BytesSentTotal = sc.CurrentValue;
                    }
                    var rc = getCounter("Bytes Received from Replica/sec", instanceName);
                    if (rc != null)
                    {
                        r.BytesReceivedPerSecond = rc.CalculatedValue;
                        r.BytesReceivedTotal = rc.CurrentValue;
                    }
                });
                return groups;
            }