Skip to content

Fix bulk insert for EF Core JSON complex collections (ComplexCollection.ToJson)#107

Draft
Copilot wants to merge 4 commits into
mainfrom
copilot/phenx-fix-bulk-insert-issue
Draft

Fix bulk insert for EF Core JSON complex collections (ComplexCollection.ToJson)#107
Copilot wants to merge 4 commits into
mainfrom
copilot/phenx-fix-bulk-insert-issue

Conversation

Copilot AI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Bulk inserting entities with ComplexCollection(...).ToJson() mappings failed with ArgumentException: Property 'X' is not defined for type 'List<T>' because TableMetadata.GetColumns tried to expand the element-type sub-properties onto the collection type.

Core fix

TableMetadata.GetColumns now detects JSON-mapped complex properties via the Relational:ContainerColumnName EF Core annotation and emits a single ColumnMetadata for the whole JSON column instead of expanding sub-properties.

ColumnMetadata gains a second constructor for JSON complex properties that serializes the collection with System.Text.Json default options (PascalCase — matching EF Core's storage format). Property is now IProperty?.

// Now works — previously threw ArgumentException during metadata construction
builder.ComplexCollection(x => x.DemandLegalEntityDistributions, x => x.ToJson());
await dbSet.ExecuteBulkInsertAsync(entities);

Provider fixes

  • PostgreSQLGetPostgreSqlType guards column.Property != null; falls back to parsing StoreDefinition to resolve NpgsqlDbType.Jsonb / NpgsqlDbType.Json.
  • SQLiteGetSqliteType guards column.Property != null; falls back to StoreDefinition prefix for the store type.

Tests

Added TestEntityWithJsonComplexCollection and an InsertsEntities_WithJsonComplexCollection test covering both null and non-null JSON collections. InsertReturn/InsertReturnAsync strategies are skipped on SQLite — EF Core requires composing JSON path expressions over the result, which is incompatible with a raw INSERT...RETURNING statement.

Copilot AI changed the title [WIP] Fix PhenX bulk insert for EF Core JSON complex collection Fix bulk insert for EF Core JSON complex collections (ComplexCollection.ToJson) Jun 9, 2026
Copilot finished work on behalf of PhenX June 9, 2026 19:55
Copilot AI requested a review from PhenX June 9, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PhenX bulk insert fails for EF Core JSON complex collection mapped with ComplexCollection(...).ToJson()

2 participants