Open r-Larch opened 8 months ago
In my commit I did a small hackfix to solve the issue for me.
This code probably only works in my case for BulkInsert
on PostgreSQL
but it may serve as a starting point to solve the issue.
It looks like the context.Model.GetRelationalModel()
contains all the required information.
var table = context.Model.GetRelationalModel().Tables.First(_ => _.Name == TableName);
string column = table.FindColumn(columnName)!;
string columnType = column.StoreType;
string sqlValue = column.StoreTypeMapping.GenerateProviderValueSqlLiteral(value).Trim('\'');
ColumnName can be obtained using:
INavigation jsonProperty = ..
string columnName = (string) jsonProperty.TargetEntityType.GetAnnotation("Relational:ContainerColumnName").Value!;
I won't make a pull request with my commit because it probably breaks other functionality.
Best regards René
@borisdj Any plans of an official fix for this (not requesting/pressuring, I'm already very grateful for this library, just curious if it was on the radar)? I'm not yet knowledgeable enough to confidently fix it myself or understand the potential side effects of the patch above. Just checking because if there's no fix planned I'll just refactor my model to not use owned jsonb columns for now. Thanks!
Will try to work it out in the following days.
@borisdj I have hit this issue too. It looks like JSON columns (configured using Own Entity Mapping
) are skipped when building the temporary table and later on when the values are being inserted into the temp table, I get this error because the column was skipped and does not exist in TableInfo.PropertyColumnNamesDict
.
System.Collections.Generic.KeyNotFoundException: The given key 'Test_Json_Column_name' was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at EFCore.BulkExtensions.SqlAdapters.PostgreSql.PostgreSqlAdapter.InsertAsync[T](DbContext context, IEnumerable`1 entities, TableInfo tableInfo, Action`1 progress, Boolean isAsync, CancellationToken cancellationToken)
Would the support be added for this?
PostgreSQL: Failes to
BulkInsert
entity with JSONb column configured with recommendedOwned Entity Mapping
.It looks like EFCore.BulkExtensions handles the Jsonb columns like a relation navigation property but it is configured as
Owned Entity
with conversion to JSON.The property is configured with:
builder.OwnsOne(_ => _.Metadata, _ => _.ToJson());
docs:npgsql.orgThis code throws the exception:
Exception: (Metadata is a Jsonb column not a relation)
Entity with configured Jsonb column:
EF Context Setup for PostgreSQL