|
| 1 | +# Graph Insert (Navigation Properties) |
| 2 | + |
| 3 | +This library supports bulk inserting entire object graphs, including entities with their related navigation properties. |
| 4 | + |
| 5 | +## Enabling Graph Insert |
| 6 | + |
| 7 | +```csharp |
| 8 | +await dbContext.ExecuteBulkInsertAsync(blogs, options => |
| 9 | +{ |
| 10 | + options.IncludeGraph = true; |
| 11 | +}); |
| 12 | +``` |
| 13 | + |
| 14 | +## How It Works |
| 15 | + |
| 16 | +1. The library traverses all reachable entities via navigation properties |
| 17 | +2. Entities are sorted in topological order (parents before children) to respect foreign key constraints |
| 18 | +3. Each entity type is bulk inserted in dependency order |
| 19 | +4. Generated IDs (identity columns) are propagated to foreign key properties |
| 20 | +5. Many-to-many join tables are populated automatically |
| 21 | + |
| 22 | +## Options |
| 23 | + |
| 24 | +| Option | Default | Description | |
| 25 | +|--------|---------|-------------| |
| 26 | +| `IncludeGraph` | `false` | Enable graph traversal | |
| 27 | +| `MaxGraphDepth` | `0` (unlimited) | Maximum depth to traverse. Use 0 for unlimited. | |
| 28 | +| `IncludeNavigations` | `null` (all) | Specific navigation property names to include | |
| 29 | +| `ExcludeNavigations` | `null` (none) | Navigation property names to exclude | |
| 30 | + |
| 31 | +## Supported Relationship Types |
| 32 | + |
| 33 | +- ✅ One-to-Many (e.g., Blog → Posts) |
| 34 | +- ✅ Many-to-One (e.g., Post → Blog) |
| 35 | +- ✅ One-to-One (e.g., Blog → BlogSettings) |
| 36 | +- ✅ Many-to-Many with join table (e.g., Post ↔ Tags) |
| 37 | +- ✅ Self-referencing/Hierarchies (e.g., Category → Parent/Children) |
| 38 | + |
| 39 | +## Performance Considerations |
| 40 | + |
| 41 | +- 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 |
| 43 | +- Consider using client-generated keys (GUIDs with `ValueGeneratedNever()`) to avoid ID propagation overhead |
| 44 | +- Use `MaxGraphDepth` to limit traversal for large/deep graphs |
| 45 | +- Use `IncludeNavigations` or `ExcludeNavigations` to reduce the scope of insertions |
| 46 | + |
| 47 | +## Example |
| 48 | + |
| 49 | +### One-to-Many Relationship |
| 50 | + |
| 51 | +```csharp |
| 52 | +var blog = new Blog |
| 53 | +{ |
| 54 | + Name = "My Blog", |
| 55 | + Posts = new List<Post> |
| 56 | + { |
| 57 | + new Post { Title = "First Post" }, |
| 58 | + new Post { Title = "Second Post" } |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +await dbContext.ExecuteBulkInsertAsync(new[] { blog }, o => o.IncludeGraph = true); |
| 63 | + |
| 64 | +// After insert: |
| 65 | +// - blog.Id is populated |
| 66 | +// - blog.Posts[0].BlogId == blog.Id |
| 67 | +// - blog.Posts[1].BlogId == blog.Id |
| 68 | +``` |
| 69 | + |
| 70 | +### One-to-One Relationship |
| 71 | + |
| 72 | +```csharp |
| 73 | +var blog = new Blog |
| 74 | +{ |
| 75 | + Name = "My Blog", |
| 76 | + Settings = new BlogSettings { EnableComments = true } |
| 77 | +}; |
| 78 | + |
| 79 | +await dbContext.ExecuteBulkInsertAsync(new[] { blog }, o => o.IncludeGraph = true); |
| 80 | + |
| 81 | +// After insert: |
| 82 | +// - blog.Id is populated |
| 83 | +// - blog.Settings.BlogId == blog.Id |
| 84 | +``` |
| 85 | + |
| 86 | +### Selective Navigation Inclusion |
| 87 | + |
| 88 | +```csharp |
| 89 | +var blog = new Blog |
| 90 | +{ |
| 91 | + Name = "My Blog", |
| 92 | + Posts = new List<Post> { new Post { Title = "Post" } }, |
| 93 | + Settings = new BlogSettings { EnableComments = true } |
| 94 | +}; |
| 95 | + |
| 96 | +// Only insert Posts, not Settings |
| 97 | +await dbContext.ExecuteBulkInsertAsync(new[] { blog }, o => |
| 98 | +{ |
| 99 | + o.IncludeGraph = true; |
| 100 | + o.IncludeNavigations = new HashSet<string> { "Posts" }; |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +### Limiting Graph Depth |
| 105 | + |
| 106 | +```csharp |
| 107 | +var blog = new Blog |
| 108 | +{ |
| 109 | + Name = "My Blog", |
| 110 | + Posts = new List<Post> |
| 111 | + { |
| 112 | + new Post |
| 113 | + { |
| 114 | + Title = "Post", |
| 115 | + Tags = new List<Tag> { new Tag { Name = "EF Core" } } // Won't be inserted |
| 116 | + } |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +// MaxGraphDepth = 1 means only Blog and direct children (Posts) |
| 121 | +await dbContext.ExecuteBulkInsertAsync(new[] { blog }, o => |
| 122 | +{ |
| 123 | + o.IncludeGraph = true; |
| 124 | + o.MaxGraphDepth = 1; |
| 125 | +}); |
| 126 | +``` |
| 127 | + |
| 128 | +## Limitations |
| 129 | + |
| 130 | +- **Shadow foreign keys**: Currently not supported. Add a CLR property for foreign keys. |
| 131 | +- **Circular references**: Handled gracefully by tracking visited entities, but may result in incomplete graphs. |
| 132 | +- **OnConflict/Upsert**: Not currently supported with `IncludeGraph = true`. |
0 commit comments