|
5 | 5 | using Microsoft.Extensions.Logging; |
6 | 6 |
|
7 | 7 | using PhenX.EntityFrameworkCore.BulkInsert.Abstractions; |
| 8 | +using PhenX.EntityFrameworkCore.BulkInsert.Extensions; |
8 | 9 | using PhenX.EntityFrameworkCore.BulkInsert.Metadata; |
9 | 10 | using PhenX.EntityFrameworkCore.BulkInsert.Options; |
10 | 11 |
|
@@ -37,6 +38,10 @@ public async Task<GraphInsertResult<T>> InsertGraph<T>( |
37 | 38 | IBulkInsertProvider provider, |
38 | 39 | CancellationToken ctk) where T : class |
39 | 40 | { |
| 41 | + |
| 42 | + using var activity = Telemetry.ActivitySource.StartActivity("InsertGraph"); |
| 43 | + activity?.AddTag("synchronous", sync); |
| 44 | + |
40 | 45 | // 1. Collect and sort entities |
41 | 46 | var collector = new GraphEntityCollector(_context, options); |
42 | 47 | var collectionResult = collector.Collect(entities); |
@@ -68,42 +73,54 @@ public async Task<GraphInsertResult<T>> InsertGraph<T>( |
68 | 73 | $"Consider using client-generated keys (e.g., GUIDs with ValueGeneratedNever())."); |
69 | 74 | } |
70 | 75 |
|
71 | | - // 2. Insert in dependency order (parents first) |
72 | | - foreach (var entityType in collectionResult.InsertionOrder) |
| 76 | + var connection = await _context.GetConnection(sync, ctk); |
| 77 | + |
| 78 | + try |
73 | 79 | { |
74 | | - if (!collectionResult.EntitiesByType.TryGetValue(entityType, out var entitiesToInsert) || |
75 | | - entitiesToInsert.Count == 0) |
| 80 | + // 2. Insert in dependency order (parents first) |
| 81 | + foreach (var entityType in collectionResult.InsertionOrder) |
76 | 82 | { |
77 | | - continue; |
78 | | - } |
| 83 | + if (!collectionResult.EntitiesByType.TryGetValue(entityType, out var entitiesToInsert) || |
| 84 | + entitiesToInsert.Count == 0) |
| 85 | + { |
| 86 | + continue; |
| 87 | + } |
79 | 88 |
|
80 | | - // Propagate FK values from already-inserted parents |
81 | | - PropagateParentForeignKeys(entitiesToInsert, entityType, graphMetadata); |
| 89 | + // Propagate FK values from already-inserted parents |
| 90 | + PropagateParentForeignKeys(entitiesToInsert, entityType, graphMetadata); |
82 | 91 |
|
83 | | - // Insert entities of this type |
84 | | - await InsertEntitiesOfType(sync, _context, entityType, entitiesToInsert, options, provider, graphMetadata, ctk); |
| 92 | + // Insert entities of this type |
| 93 | + await InsertEntitiesOfType(sync, _context, entityType, entitiesToInsert, options, provider, |
| 94 | + graphMetadata, ctk); |
85 | 95 |
|
86 | | - totalInserted += entitiesToInsert.Count; |
87 | | - } |
| 96 | + totalInserted += entitiesToInsert.Count; |
| 97 | + } |
88 | 98 |
|
89 | | - // 3. Insert join table records for many-to-many relationships |
90 | | - var joinRecordsInserted = 0; |
91 | | - if (collectionResult.JoinRecords.Count > 0) |
92 | | - { |
93 | | - joinRecordsInserted = await InsertJoinRecords(sync, _context, collectionResult.JoinRecords, options, provider, graphMetadata, ctk); |
94 | | - totalInserted += joinRecordsInserted; |
95 | | - } |
| 99 | + // 3. Insert join table records for many-to-many relationships |
| 100 | + if (collectionResult.JoinRecords.Count > 0) |
| 101 | + { |
| 102 | + totalInserted += await InsertJoinRecords(sync, _context, collectionResult.JoinRecords, options, |
| 103 | + provider, graphMetadata, ctk); |
| 104 | + } |
| 105 | + |
| 106 | + // Return root entities |
| 107 | + var rootEntities = collectionResult.EntitiesByType.TryGetValue(typeof(T), out var roots) |
| 108 | + ? roots.Cast<T>().ToList() |
| 109 | + : []; |
96 | 110 |
|
97 | | - // Return root entities |
98 | | - var rootEntities = collectionResult.EntitiesByType.TryGetValue(typeof(T), out var roots) |
99 | | - ? roots.Cast<T>().ToList() |
100 | | - : []; |
| 111 | + // Commit the transaction if we own them. |
| 112 | + await connection.Commit(sync, ctk); |
101 | 113 |
|
102 | | - return new GraphInsertResult<T> |
| 114 | + return new GraphInsertResult<T> |
| 115 | + { |
| 116 | + RootEntities = rootEntities, |
| 117 | + TotalInsertedCount = totalInserted, |
| 118 | + }; |
| 119 | + } |
| 120 | + finally |
103 | 121 | { |
104 | | - RootEntities = rootEntities, |
105 | | - TotalInsertedCount = totalInserted, |
106 | | - }; |
| 122 | + await connection.Close(sync, ctk); |
| 123 | + } |
107 | 124 | } |
108 | 125 |
|
109 | 126 | private static void PropagateParentForeignKeys( |
|
0 commit comments