| | | 1 | | using System.Text; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Microsoft.EntityFrameworkCore.Infrastructure; |
| | | 4 | | using Microsoft.EntityFrameworkCore.Metadata; |
| | | 5 | | using System.Linq.Expressions; |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Elsa.Persistence.EFCore.Extensions; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides extension methods to perform bulk upsert operations for entities |
| | | 12 | | /// in an Entity Framework Core context, supporting multiple database providers. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class BulkUpsertExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Performs a bulk upsert operation on a list of entities in the specified database context using a key selector. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="TDbContext">The type of the database context.</typeparam> |
| | | 20 | | /// <typeparam name="TEntity">The type of the entity being upserted.</typeparam> |
| | | 21 | | /// <param name="dbContext">The database context where the bulk upsert operation will be executed.</param> |
| | | 22 | | /// <param name="entities">The list of entities to be upserted.</param> |
| | | 23 | | /// <param name="keySelector">An expression used to determine the key for upsert operations.</param> |
| | | 24 | | /// <param name="cancellationToken">A token to observe while waiting for the operation to complete.</param> |
| | | 25 | | public static async Task BulkUpsertAsync<TDbContext, TEntity>( |
| | | 26 | | this TDbContext dbContext, |
| | | 27 | | IList<TEntity> entities, |
| | | 28 | | Expression<Func<TEntity, string>> keySelector, |
| | | 29 | | CancellationToken cancellationToken = default) |
| | | 30 | | where TDbContext : DbContext |
| | | 31 | | where TEntity : class, new() |
| | | 32 | | { |
| | 910 | 33 | | await BulkUpsertAsync(dbContext, entities, keySelector, 50, cancellationToken); |
| | 910 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Performs a bulk upsert operation on a list of entities in the specified database context using a key selector an |
| | | 38 | | /// </summary> |
| | | 39 | | /// <typeparam name="TDbContext">The type of the database context.</typeparam> |
| | | 40 | | /// <typeparam name="TEntity">The type of the entity being upserted.</typeparam> |
| | | 41 | | /// <param name="dbContext">The database context where the bulk upsert operation will be executed.</param> |
| | | 42 | | /// <param name="entities">The list of entities to be upserted.</param> |
| | | 43 | | /// <param name="keySelector">An expression used to determine the key for upsert operations.</param> |
| | | 44 | | /// <param name="batchSize">The size of each batch for processing the upsert operation. Defaults to 50.</param> |
| | | 45 | | /// <param name="cancellationToken">A token to observe while waiting for the operation to complete.</param> |
| | | 46 | | /// <exception cref="NotSupportedException">Thrown if the database provider for the context is not supported.</excep |
| | | 47 | | public static async Task BulkUpsertAsync<TDbContext, TEntity>( |
| | | 48 | | this TDbContext dbContext, |
| | | 49 | | IList<TEntity> entities, |
| | | 50 | | Expression<Func<TEntity, string>> keySelector, |
| | | 51 | | int batchSize = 50, |
| | | 52 | | CancellationToken cancellationToken = default) |
| | | 53 | | where TDbContext : DbContext |
| | | 54 | | where TEntity : class, new() |
| | | 55 | | { |
| | 910 | 56 | | if (entities.Count == 0) |
| | 0 | 57 | | return; |
| | | 58 | | |
| | | 59 | | // Identify the current provider (e.g., "Microsoft.EntityFrameworkCore.SqlServer") |
| | 910 | 60 | | var providerName = dbContext.Database.ProviderName?.ToLowerInvariant() ?? string.Empty; |
| | | 61 | | |
| | | 62 | | // Determine the method for generating SQL based on the provider |
| | 910 | 63 | | Func<DbContext, IList<TEntity>, Expression<Func<TEntity, string>>, (string, object[])> generateSql = providerNam |
| | 910 | 64 | | { |
| | 910 | 65 | | var pn when pn.Contains("sqlserver") => GenerateSqlServerUpsert, |
| | 910 | 66 | | var pn when pn.Contains("sqlite") => GenerateSqliteUpsert, |
| | 1820 | 67 | | var pn when pn.Contains("postgres") => GeneratePostgresUpsert, |
| | 0 | 68 | | var pn when pn.Contains("mysql") => GenerateMySqlUpsert, |
| | 0 | 69 | | var pn when pn.Contains("oracle") => GenerateOracleUpsert, |
| | 0 | 70 | | _ => throw new NotSupportedException($"Provider '{providerName}' is not supported.") |
| | 910 | 71 | | }; |
| | | 72 | | |
| | | 73 | | // Loop through batched entities |
| | 3640 | 74 | | foreach (var batch in entities.Chunk(batchSize)) |
| | | 75 | | { |
| | | 76 | | // Generate SQL and parameters |
| | 910 | 77 | | var (sql, parameters) = generateSql(dbContext, batch, keySelector); |
| | | 78 | | |
| | 910 | 79 | | await dbContext.Database.ExecuteSqlRawAsync(sql, parameters, cancellationToken); |
| | | 80 | | } |
| | 910 | 81 | | } |
| | | 82 | | |
| | | 83 | | private static (string, object[]) GenerateSqlServerUpsert<TEntity>( |
| | | 84 | | DbContext dbContext, |
| | | 85 | | IList<TEntity> entities, |
| | | 86 | | Expression<Func<TEntity, string>> keySelector) |
| | | 87 | | where TEntity : class |
| | | 88 | | { |
| | 0 | 89 | | var entityType = dbContext.Model.FindEntityType(typeof(TEntity))!; |
| | 0 | 90 | | var tableName = $"[{entityType.GetSchema()}].[{entityType.GetTableName()}]"; |
| | 0 | 91 | | var storeObject = StoreObjectIdentifier.Table(entityType.GetTableName()!, entityType.GetSchema()); |
| | 0 | 92 | | var props = entityType.GetProperties().ToList(); |
| | 0 | 93 | | var keyProp = entityType.FindProperty(keySelector.GetMemberAccess().Name)!; |
| | 0 | 94 | | var keyColumnName = $"[{keyProp.GetColumnName(storeObject)}]"; |
| | 0 | 95 | | var columnNames = props |
| | 0 | 96 | | .Select(p => $"[{p.GetColumnName(storeObject)}]") |
| | 0 | 97 | | .ToList(); |
| | | 98 | | |
| | 0 | 99 | | var mergeSql = new StringBuilder(); |
| | 0 | 100 | | mergeSql.AppendLine($"MERGE {tableName} AS Target"); |
| | 0 | 101 | | mergeSql.AppendLine("USING (VALUES"); |
| | | 102 | | |
| | 0 | 103 | | var parameters = new List<object>(); |
| | 0 | 104 | | var parameterCount = 0; |
| | | 105 | | |
| | 0 | 106 | | for (var i = 0; i < entities.Count; i++) |
| | | 107 | | { |
| | 0 | 108 | | var entity = entities[i]; |
| | 0 | 109 | | var values = new List<string>(); |
| | | 110 | | |
| | 0 | 111 | | foreach (var property in props) |
| | | 112 | | { |
| | 0 | 113 | | var paramName = $"{{{parameterCount++}}}"; |
| | | 114 | | |
| | | 115 | | // If it's a shadow property, retrieve value via Entry(..).Property(..) |
| | 0 | 116 | | var value = property.IsShadowProperty() |
| | 0 | 117 | | ? dbContext.Entry(entity).Property(property.Name).CurrentValue |
| | 0 | 118 | | : property.PropertyInfo?.GetValue(entity); |
| | | 119 | | |
| | 0 | 120 | | var converter = property.GetTypeMapping().Converter; |
| | 0 | 121 | | if (converter != null) |
| | 0 | 122 | | value = converter.ConvertToProvider(value)!; |
| | | 123 | | |
| | | 124 | | // Explicitly cast null values for varbinary columns |
| | 0 | 125 | | if (property.GetColumnType().StartsWith("varbinary", StringComparison.OrdinalIgnoreCase) && value is nul |
| | 0 | 126 | | values.Add("CAST(NULL AS varbinary(max))"); // Explicitly cast null |
| | | 127 | | else |
| | 0 | 128 | | values.Add(paramName); |
| | | 129 | | |
| | 0 | 130 | | parameters.Add(value!); |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | var line = $"({string.Join(", ", values)}){(i < entities.Count - 1 ? "," : string.Empty)}"; |
| | 0 | 134 | | mergeSql.AppendLine(line); |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | mergeSql.AppendLine($") AS Source ({string.Join(", ", columnNames)})"); |
| | 0 | 138 | | mergeSql.AppendLine($"ON Target.{keyColumnName} = Source.{keyColumnName}"); |
| | 0 | 139 | | mergeSql.AppendLine("WHEN MATCHED THEN"); |
| | 0 | 140 | | mergeSql.AppendLine($" UPDATE SET {string.Join(", ", columnNames.Where(c => c != keyColumnName).Select(c => $ |
| | 0 | 141 | | mergeSql.AppendLine("WHEN NOT MATCHED THEN"); |
| | 0 | 142 | | mergeSql.AppendLine($" INSERT ({string.Join(", ", columnNames)})"); |
| | 0 | 143 | | mergeSql.AppendLine($" VALUES ({string.Join(", ", columnNames.Select(c => $"Source.{c}"))});"); |
| | | 144 | | |
| | 0 | 145 | | return (mergeSql.ToString(), parameters.ToArray()); |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | private static (string, object[]) GenerateSqliteUpsert<TEntity>( |
| | | 149 | | DbContext dbContext, |
| | | 150 | | IList<TEntity> entities, |
| | | 151 | | Expression<Func<TEntity, string>> keySelector) |
| | | 152 | | where TEntity : class |
| | | 153 | | { |
| | 0 | 154 | | var entityType = dbContext.Model.FindEntityType(typeof(TEntity))!; |
| | 0 | 155 | | var tableName = entityType.GetTableName(); |
| | 0 | 156 | | var storeObject = StoreObjectIdentifier.Table(tableName!, entityType.GetSchema()); |
| | 0 | 157 | | var props = entityType.GetProperties().ToList(); |
| | 0 | 158 | | var keyProp = entityType.FindProperty(keySelector.GetMemberAccess().Name)!; |
| | 0 | 159 | | var keyColumnName = keyProp.GetColumnName(storeObject); |
| | 0 | 160 | | var columnNames = props |
| | 0 | 161 | | .Select(p => p.GetColumnName(storeObject)!) |
| | 0 | 162 | | .ToList(); |
| | | 163 | | |
| | 0 | 164 | | var sb = new StringBuilder(); |
| | 0 | 165 | | var parameters = new List<object>(); |
| | 0 | 166 | | var parameterCount = 0; |
| | | 167 | | |
| | 0 | 168 | | sb.Append($"INSERT INTO \"{tableName}\" ({string.Join(", ", columnNames.Select(c => $"\"{c}\""))}) VALUES "); |
| | | 169 | | |
| | 0 | 170 | | for (var i = 0; i < entities.Count; i++) |
| | | 171 | | { |
| | 0 | 172 | | var entity = entities[i]; |
| | 0 | 173 | | var placeholders = new List<string>(); |
| | | 174 | | |
| | 0 | 175 | | foreach (var property in props) |
| | | 176 | | { |
| | 0 | 177 | | var paramName = $"{{{parameterCount++}}}"; |
| | | 178 | | |
| | 0 | 179 | | var value = property.IsShadowProperty() |
| | 0 | 180 | | ? dbContext.Entry(entity).Property(property.Name).CurrentValue |
| | 0 | 181 | | : property.PropertyInfo?.GetValue(entity); |
| | | 182 | | |
| | 0 | 183 | | var converter = property.GetTypeMapping().Converter; |
| | 0 | 184 | | if (converter != null) |
| | 0 | 185 | | value = converter.ConvertToProvider(value); |
| | | 186 | | |
| | 0 | 187 | | placeholders.Add(paramName); |
| | 0 | 188 | | parameters.Add(value!); |
| | | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | sb.Append($"({string.Join(", ", placeholders)})"); |
| | 0 | 192 | | if (i < entities.Count - 1) |
| | 0 | 193 | | sb.Append(", "); |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | sb.AppendLine(); |
| | 0 | 197 | | sb.AppendLine($"ON CONFLICT(\"{keyColumnName}\") DO UPDATE SET"); |
| | | 198 | | |
| | 0 | 199 | | var updateAssignments = columnNames |
| | 0 | 200 | | .Where(c => c != keyColumnName) |
| | 0 | 201 | | .Select(c => $"\"{c}\"=excluded.\"{c}\""); |
| | | 202 | | |
| | 0 | 203 | | sb.AppendLine(string.Join(", ", updateAssignments) + ";"); |
| | | 204 | | |
| | 0 | 205 | | return (sb.ToString(), parameters.ToArray()); |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | private static (string, object[]) GeneratePostgresUpsert<TEntity>( |
| | | 209 | | DbContext dbContext, |
| | | 210 | | IList<TEntity> entities, |
| | | 211 | | Expression<Func<TEntity, string>> keySelector) |
| | | 212 | | where TEntity : class |
| | | 213 | | { |
| | 910 | 214 | | var entityType = dbContext.Model.FindEntityType(typeof(TEntity))!; |
| | 910 | 215 | | var tableName = entityType.GetTableName(); |
| | 910 | 216 | | var storeObject = StoreObjectIdentifier.Table(tableName!, entityType.GetSchema()); |
| | | 217 | | |
| | 910 | 218 | | var props = entityType.GetProperties().ToList(); |
| | | 219 | | |
| | 910 | 220 | | var keyProp = entityType.FindProperty(keySelector.GetMemberAccess().Name)!; |
| | 910 | 221 | | var keyColumnName = keyProp.GetColumnName(storeObject); |
| | 910 | 222 | | var columnNames = props |
| | 18044 | 223 | | .Select(p => p.GetColumnName(storeObject)!) |
| | 910 | 224 | | .ToList(); |
| | | 225 | | |
| | 910 | 226 | | var sb = new StringBuilder(); |
| | 910 | 227 | | var parameters = new List<object>(); |
| | 910 | 228 | | var parameterCount = 0; |
| | | 229 | | |
| | 18954 | 230 | | sb.Append($"INSERT INTO \"{storeObject.Schema}\".\"{storeObject.Name}\" ({string.Join(", ", columnNames.Select(c |
| | | 231 | | |
| | 7962 | 232 | | for (var i = 0; i < entities.Count; i++) |
| | | 233 | | { |
| | 3071 | 234 | | var entity = entities[i]; |
| | 3071 | 235 | | var placeholders = new List<string>(); |
| | | 236 | | |
| | 128670 | 237 | | foreach (var property in props) |
| | | 238 | | { |
| | 61264 | 239 | | var paramName = $"{{{parameterCount++}}}"; |
| | | 240 | | |
| | 61264 | 241 | | var value = property.IsShadowProperty() |
| | 61264 | 242 | | ? dbContext.Entry(entity).Property(property.Name).CurrentValue |
| | 61264 | 243 | | : property.PropertyInfo?.GetValue(entity); |
| | | 244 | | |
| | 61264 | 245 | | var converter = property.GetTypeMapping().Converter; |
| | 61264 | 246 | | if (converter != null) |
| | 3058 | 247 | | value = converter.ConvertToProvider(value); |
| | | 248 | | |
| | | 249 | | // Detect json/jsonb column types and cast the parameter so PostgreSQL accepts it. |
| | 61264 | 250 | | var columnType = property.GetColumnType(); |
| | 61264 | 251 | | if (columnType.StartsWith("jsonb", StringComparison.OrdinalIgnoreCase)) |
| | 577 | 252 | | placeholders.Add($"CAST({paramName} AS jsonb)"); |
| | 60687 | 253 | | else if (columnType.StartsWith("json", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 254 | | placeholders.Add($"CAST({paramName} AS json)"); |
| | | 255 | | else |
| | 60687 | 256 | | placeholders.Add(paramName); |
| | | 257 | | |
| | 61264 | 258 | | parameters.Add(value!); |
| | | 259 | | } |
| | | 260 | | |
| | 3071 | 261 | | sb.Append($"({string.Join(", ", placeholders)})"); |
| | 3071 | 262 | | if (i < entities.Count - 1) |
| | 2161 | 263 | | sb.Append(", "); |
| | | 264 | | } |
| | | 265 | | |
| | 910 | 266 | | sb.AppendLine(); |
| | 910 | 267 | | sb.AppendLine($"ON CONFLICT (\"{keyColumnName}\") DO UPDATE SET"); |
| | | 268 | | |
| | 910 | 269 | | var updateAssignments = columnNames |
| | 18044 | 270 | | .Where(c => c != keyColumnName) |
| | 18044 | 271 | | .Select(c => $"\"{c}\" = EXCLUDED.\"{c}\""); |
| | | 272 | | |
| | 910 | 273 | | sb.AppendLine(string.Join(", ", updateAssignments) + ";"); |
| | | 274 | | |
| | 910 | 275 | | return (sb.ToString(), parameters.ToArray()); |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | private static (string, object[]) GenerateMySqlUpsert<TEntity>( |
| | | 279 | | DbContext dbContext, |
| | | 280 | | IList<TEntity> entities, |
| | | 281 | | Expression<Func<TEntity, string>> keySelector) |
| | | 282 | | where TEntity : class |
| | | 283 | | { |
| | 0 | 284 | | var entityType = dbContext.Model.FindEntityType(typeof(TEntity))!; |
| | 0 | 285 | | var tableName = entityType.GetTableName(); |
| | 0 | 286 | | var storeObject = StoreObjectIdentifier.Table(tableName!, entityType.GetSchema()); |
| | | 287 | | |
| | 0 | 288 | | var props = entityType.GetProperties().ToList(); |
| | | 289 | | |
| | 0 | 290 | | var keyProp = entityType.FindProperty(keySelector.GetMemberAccess().Name)!; |
| | 0 | 291 | | var keyColumnName = keyProp.GetColumnName(storeObject); |
| | 0 | 292 | | var columnNames = props |
| | 0 | 293 | | .Select(p => p.GetColumnName(storeObject)!) |
| | 0 | 294 | | .ToList(); |
| | | 295 | | |
| | 0 | 296 | | var sb = new StringBuilder(); |
| | 0 | 297 | | var parameters = new List<object>(); |
| | 0 | 298 | | var parameterCount = 0; |
| | | 299 | | |
| | 0 | 300 | | sb.Append($"INSERT INTO `{tableName}` ({string.Join(", ", columnNames.Select(c => $"`{c}`"))}) VALUES "); |
| | | 301 | | |
| | 0 | 302 | | for (var i = 0; i < entities.Count; i++) |
| | | 303 | | { |
| | 0 | 304 | | var entity = entities[i]; |
| | 0 | 305 | | var placeholders = new List<string>(); |
| | | 306 | | |
| | 0 | 307 | | foreach (var property in props) |
| | | 308 | | { |
| | 0 | 309 | | var paramName = $"{{{parameterCount++}}}"; |
| | | 310 | | |
| | 0 | 311 | | var value = property.IsShadowProperty() |
| | 0 | 312 | | ? dbContext.Entry(entity).Property(property.Name).CurrentValue |
| | 0 | 313 | | : property.PropertyInfo?.GetValue(entity); |
| | | 314 | | |
| | 0 | 315 | | var converter = property.GetTypeMapping().Converter; |
| | 0 | 316 | | if (converter != null) |
| | 0 | 317 | | value = converter.ConvertToProvider(value); |
| | | 318 | | |
| | 0 | 319 | | placeholders.Add(paramName); |
| | 0 | 320 | | parameters.Add(value!); |
| | | 321 | | } |
| | | 322 | | |
| | 0 | 323 | | sb.Append($"({string.Join(", ", placeholders)})"); |
| | 0 | 324 | | if (i < entities.Count - 1) |
| | 0 | 325 | | sb.Append(", "); |
| | | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | sb.AppendLine(); |
| | 0 | 329 | | sb.AppendLine("ON DUPLICATE KEY UPDATE"); |
| | | 330 | | |
| | 0 | 331 | | var updateAssignments = columnNames |
| | 0 | 332 | | .Where(c => c != keyColumnName) |
| | 0 | 333 | | .Select(c => $"`{c}` = VALUES(`{c}`)"); |
| | | 334 | | |
| | 0 | 335 | | sb.AppendLine(string.Join(", ", updateAssignments) + ";"); |
| | | 336 | | |
| | 0 | 337 | | return (sb.ToString(), parameters.ToArray()); |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private static (string, object[]) GenerateOracleUpsert<TEntity>( |
| | | 341 | | DbContext dbContext, |
| | | 342 | | IList<TEntity> entities, |
| | | 343 | | Expression<Func<TEntity, string>> keySelector) |
| | | 344 | | where TEntity : class |
| | | 345 | | { |
| | 0 | 346 | | var entityType = dbContext.Model.FindEntityType(typeof(TEntity))!; |
| | 0 | 347 | | var schema = entityType.GetSchema(); |
| | 0 | 348 | | var tableName = entityType.GetTableName(); |
| | 0 | 349 | | var storeObject = StoreObjectIdentifier.Table(tableName!, schema); |
| | 0 | 350 | | var fullName = !string.IsNullOrEmpty(schema) ? $"{schema}.{tableName}" : tableName; |
| | | 351 | | |
| | 0 | 352 | | var props = entityType.GetProperties().ToList(); |
| | | 353 | | |
| | 0 | 354 | | var keyProp = entityType.FindProperty(keySelector.GetMemberAccess().Name)!; |
| | 0 | 355 | | var keyColumnName = keyProp.GetColumnName(storeObject); |
| | | 356 | | |
| | 0 | 357 | | var columnNames = props |
| | 0 | 358 | | .Select(p => p.GetColumnName(storeObject)!) |
| | 0 | 359 | | .ToList(); |
| | | 360 | | |
| | 0 | 361 | | var sb = new StringBuilder(); |
| | 0 | 362 | | var parameters = new List<object>(); |
| | 0 | 363 | | var parameterCount = 0; |
| | | 364 | | |
| | 0 | 365 | | sb.AppendLine($"MERGE INTO {fullName} Target"); |
| | 0 | 366 | | sb.AppendLine("USING (SELECT"); |
| | | 367 | | |
| | 0 | 368 | | for (var i = 0; i < entities.Count; i++) |
| | | 369 | | { |
| | 0 | 370 | | var entity = entities[i]; |
| | 0 | 371 | | var lineParts = new List<string>(); |
| | | 372 | | |
| | 0 | 373 | | foreach (var property in props) |
| | | 374 | | { |
| | 0 | 375 | | var paramName = $"{{{parameterCount++}}}"; |
| | | 376 | | |
| | 0 | 377 | | var value = property.IsShadowProperty() |
| | 0 | 378 | | ? dbContext.Entry(entity).Property(property.Name).CurrentValue |
| | 0 | 379 | | : property.PropertyInfo?.GetValue(entity); |
| | | 380 | | |
| | 0 | 381 | | var converter = property.GetTypeMapping().Converter; |
| | 0 | 382 | | if (converter != null) |
| | 0 | 383 | | value = converter.ConvertToProvider(value); |
| | | 384 | | |
| | 0 | 385 | | parameters.Add(value!); |
| | | 386 | | |
| | | 387 | | // Oracle aliases must match the column name |
| | 0 | 388 | | var alias = property.GetColumnName(storeObject); |
| | 0 | 389 | | lineParts.Add($"{paramName} AS {alias}"); |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | // Comma if not last |
| | 0 | 393 | | var suffix = (i < entities.Count - 1) ? " FROM DUAL UNION ALL SELECT" : " FROM DUAL"; |
| | 0 | 394 | | sb.AppendLine(string.Join(", ", lineParts) + suffix); |
| | | 395 | | } |
| | | 396 | | |
| | 0 | 397 | | sb.AppendLine($") Source ON (Target.{keyColumnName} = Source.{keyColumnName})"); |
| | 0 | 398 | | sb.AppendLine("WHEN MATCHED THEN UPDATE SET"); |
| | | 399 | | |
| | 0 | 400 | | var updateSetClauses = columnNames |
| | 0 | 401 | | .Where(c => c != keyColumnName) |
| | 0 | 402 | | .Select(c => $"Target.{c} = Source.{c}"); |
| | | 403 | | |
| | 0 | 404 | | sb.AppendLine(string.Join(", ", updateSetClauses)); |
| | 0 | 405 | | sb.AppendLine("WHEN NOT MATCHED THEN"); |
| | 0 | 406 | | sb.AppendLine($"INSERT ({string.Join(", ", columnNames)})"); |
| | 0 | 407 | | sb.AppendLine($"VALUES ({string.Join(", ", columnNames.Select(c => $"Source.{c}"))});"); |
| | | 408 | | |
| | 0 | 409 | | return (sb.ToString(), parameters.ToArray()); |
| | | 410 | | } |
| | | 411 | | } |