efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Jsonb[] mapping not working

Open sumit-sood-pluralsight opened this issue 1 year ago • 5 comments

Hi, I am using these packages in my .Net Core 8project :

  • Microsoft.EntityFrameworkCore 8.0.2
  • Npgsql 8.0.2
  • Npgsql.EntityFrameworkCore.PostgreSQL 8.0.2
  • EFCore.NamingConventions 8.0.3

My DBContext looks like this : services.AddDbContext<MyDbContext>( options => options.UseNpgsql(connectionStrings.ReadOnlyConnectionString) .UseSnakeCaseNamingConvention() .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)

When I am trying to map the jsonb[] (jsonb Array) using OwnsMany it's throwing error. I have added the mapping like this in the OnModelCreating : builder.Entity<SomeEntity>() .OwnsMany(auth => auth.AllContent, d => { d.ToJson("all_content"); });

I have defined my POCO class as :

public class SomeEntity 
{     
 public List<SomeContent> AllContent { get; set; } = []; 
}
public class SomeContent {   
 [JsonPropertyName("guid")] 
 public string Id { get; set; } 
[JsonPropertyName("content_type")] 
public string ContentType { get; set; } 
[JsonPropertyName("status")] 
public string Status { get; set; } 
}

When I am trying to access my Entity I am getting error : "Reading as 'System.String' is not supported for fields having DataTypeName 'jsonb[]'\nType 'String' required dynamic JSON serialization, which requires an explicit opt-in; call 'EnableDynamicJson' on 'NpgsqlDataSourceBuilder' or NpgsqlConnection.GlobalTypeMapper (see https://www.npgsql.org/doc/types/json.html and the 8.0 release notes for more details). Alternatively, if you meant to use Newtonsoft JSON.NET instead of System.Text.Json, call UseJsonNet() instead."

Is jsob[] supported currently if yes Please suggest how to access jsonb[] or am I missing something. Thanks in Advance.

sumit-sood-pluralsight avatar Mar 13 '24 05:03 sumit-sood-pluralsight

https://github.com/npgsql/efcore.pg/issues/3172#issue-2300558283

I think my problem is related to this issue.

Have you found a way to fix it?

zN3utr4l avatar May 16 '24 14:05 zN3utr4l

same issue here when migrating from .net 6 to .net 8

dragos-stoica-metro avatar Jun 10 '24 07:06 dragos-stoica-metro

https://github.com/dotnet/efcore/issues/33913 It's a bug already reported, we're waiting for the resolution

zN3utr4l avatar Jun 10 '24 11:06 zN3utr4l

I have a similar problem when migrating from .net7 to .net 8

podhale avatar Jun 13 '24 12:06 podhale

I've got a similar exception when updated the version of Npgsql.EntityFrameworkCore.PostgreSQL to 8.0.4 from 6.0.7. I rolled back to 8.0.0 And added to my dbContext

        var dataSource = new NpgsqlDataSourceBuilder(options.ConnectionString)
            .EnableDynamicJson()
            .Build();

        serviceCollection.AddDbContext<MyDbContext>(dbContextOptions => dbContextOptions.UseNpgsql(dataSource));

For 8.0.6 with EnableDynamicJson I've got System.ArgumentException: No mapping exists from object type System.Linq.Enumerable+SelectListIterator2[[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int64, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] to a known managed provider native type. at Microsoft.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed) at Microsoft.Data.SqlClient.MetaType.GetMetaTypeFromType(Type dataType) at Microsoft.Data.SqlClient.SqlParameter.GetMetaTypeOnly() at Microsoft.Data.SqlClient.SqlParameter.get_DbType() at EFCore.BulkExtensions.SqlAdapters.SqlDefaultDialect.ReloadSqlParameters(DbContext context, List1 sqlParameters) at EFCore.BulkExtensions.BatchUtil.ReloadSqlParameters(DbContext context, List`1 sqlParameters) at EFCore.BulkExtensions.BatchUtil.GetSqlDelete(IQueryable query, DbContext context) at EFCore.BulkExtensions.IQueryableBatchExtensions.GetBatchDeleteArguments(IQueryable query) at EFCore.BulkExtensions.IQueryableBatchExtensions.BatchDeleteAsync(IQueryable query, CancellationToken cancellationToken)

Updated: I have all the errors disappeared and on the latest version (8.0.4) . When I stopped using obsolete method BatchDelete from EFCore.BulkExtensions and replaced it with ExecuteDelete. But enabling EnableDynamicJson is also necessary

NikiforovTem avatar Jun 13 '24 14:06 NikiforovTem

Everyone, from the above I'm not sure if a problem still exists, and am missing a minimal, runnable repro I could check. If you still have an issue with the latest 8.0.x, please post back here with such a repro and I'll revisit.

roji avatar Oct 28 '24 09:10 roji