private static long InsertManyOneByOne <T>(this IDbConnection connection, IEnumerable <T> entitiesToInsert, TypeInfo typeInfo, TypeQueryInfo queryInfo, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { var sql = QueryHelper.GetInsertQuery(connection, typeInfo, queryInfo); if (typeInfo.KeyType == KeyType.Surrogate) { foreach (var entity in entitiesToInsert) { using (var reader = connection.QueryMultiple(sql, entity, transaction, commandTimeout)) { var keyProperty = typeInfo.PrimaryKeyProperties.First(); var res = reader.Read(); var id = res.First().id; keyProperty.SetValue(entity, Convert.ChangeType(id, keyProperty.PropertyType), null); } } return(entitiesToInsert.Count()); } else { connection.Execute(sql, entitiesToInsert, transaction, commandTimeout); return(entitiesToInsert.Count()); } }
private static long InsertManySingleShot <T>(this IDbConnection connection, IEnumerable <T> entitiesToInsert, TypeInfo typeInfo, TypeQueryInfo queryInfo, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { var sql = QueryHelper.GetInsertManyQuery(connection, typeInfo, queryInfo, entitiesToInsert.Count()); DynamicParameters dynParams = new DynamicParameters(); for (int i = 0; i < entitiesToInsert.Count(); i++) { foreach (var prop in typeInfo.InsertableProperties) { dynParams.Add(QueryHelper.GetParamName(prop, $"_{i}"), prop.GetValue(entitiesToInsert.ElementAt(i))); } } if (typeInfo.KeyType == KeyType.Surrogate) { using (var reader = connection.QueryMultiple(sql, dynParams, transaction, commandTimeout)) { var keyProperty = typeInfo.PrimaryKeyProperties.First(); foreach (var entity in entitiesToInsert) { var res = reader.Read(); var id = res.First().id; keyProperty.SetValue(entity, Convert.ChangeType(id, keyProperty.PropertyType), null); } return(entitiesToInsert.Count()); } } else { connection.Execute(sql, dynParams, transaction, commandTimeout); return(entitiesToInsert.Count()); } }