Skip to content

Commit 570bd6a

Browse files
CopilotPhenX
andcommitted
Address code review comments: add debug logging and fix docs
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
1 parent e1aa4de commit 570bd6a

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

docs/graph-insert.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ await dbContext.ExecuteBulkInsertAsync(blogs, options =>
3939
## Performance Considerations
4040

4141
- Graph insert is inherently slower than flat insert due to FK propagation overhead
42-
- For entities with identity columns, the library uses `ExecuteBulkInsertReturnEntities` internally to retrieve generated IDs
42+
- For entities with identity columns, the library uses `ExecuteBulkInsertReturnEntitiesAsync` internally to retrieve generated IDs
4343
- Consider using client-generated keys (GUIDs with `ValueGeneratedNever()`) to avoid ID propagation overhead
4444
- Use `MaxGraphDepth` to limit traversal for large/deep graphs
4545
- Use `IncludeNavigations` or `ExcludeNavigations` to reduce the scope of insertions

src/PhenX.EntityFrameworkCore.BulkInsert/Graph/GraphBulkInsertOrchestrator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,13 @@ private static void CopyGeneratedIds<TEntity>(
226226
{
227227
if (originalEntities.Count != insertedEntities.Count)
228228
{
229-
// Can't reliably map back
229+
// Count mismatch - this can happen if the bulk insert operation
230+
// doesn't preserve order. Log a warning for debugging purposes.
231+
// The graph insert will continue but FK propagation may be incomplete.
232+
System.Diagnostics.Debug.WriteLine(
233+
$"Warning: IncludeGraph ID propagation failed for {typeof(TEntity).Name}. " +
234+
$"Original count: {originalEntities.Count}, Inserted count: {insertedEntities.Count}. " +
235+
"Foreign key values may not be correctly propagated to dependent entities.");
230236
return;
231237
}
232238

@@ -288,6 +294,9 @@ private async Task InsertJoinRecordsAsync(
288294
var joinEntry = Activator.CreateInstance(joinEntityType);
289295
if (joinEntry == null)
290296
{
297+
System.Diagnostics.Debug.WriteLine(
298+
$"Warning: IncludeGraph failed to create join entry for {joinEntityType.Name}. " +
299+
"Many-to-many relationship may be incomplete.");
291300
continue;
292301
}
293302

src/PhenX.EntityFrameworkCore.BulkInsert/Graph/GraphEntityCollector.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public GraphEntityCollector(DbContext context, BulkInsertOptions options)
5555
{
5656
_options = options;
5757
_graphMetadata = new GraphMetadata(context, options);
58+
// Use ReferenceEqualityComparer to track visited entity instances by reference,
59+
// not by property values, to correctly handle cycles in the object graph
5860
_visited = new HashSet<object>(ReferenceEqualityComparer.Instance);
5961
_entitiesByType = [];
6062
_joinRecords = [];

0 commit comments

Comments
 (0)