© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago
sneki

Npgsql 8.0 + EFCore 8.0 + JSON Colums with type List<T> not working

Hello!

I'm not sure if this is a bug or intended behavior, but I'm trying to have a
Patient
Patient
entity that can have a dynamic amount of phone numbers by making its property

public List<PhoneNumber> PhoneNumbers { get; set; } = new();
public List<PhoneNumber> PhoneNumbers { get; set; } = new();


a json column. According to the docs, in version 8
[ColumnType("jsonb")]
[ColumnType("jsonb")]
has been deprecated in favor of using
.ToJson()
.ToJson()
, which I'm now trying to apply since I just upgraded from net7 and Npgsql 7 to 8. I'm setting up
EnableDynamicJson()
EnableDynamicJson()
in my DbContext as follows:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
        dataSourceBuilder.EnableDynamicJson();
        dataSourceBuilder.UseJsonNet(); // just an experiment that didn't work, but doesn't make a difference
        var dataSource = dataSourceBuilder.Build();
        
        optionsBuilder
            .UseNpgsql(dataSource)
            .UseSnakeCaseNamingConvention();
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
        dataSourceBuilder.EnableDynamicJson();
        dataSourceBuilder.UseJsonNet(); // just an experiment that didn't work, but doesn't make a difference
        var dataSource = dataSourceBuilder.Build();
        
        optionsBuilder
            .UseNpgsql(dataSource)
            .UseSnakeCaseNamingConvention();
    }


Then, as per the docs, I'm setting up my
PatientConfiguration
PatientConfiguration
as follows:

    public void Configure(EntityTypeBuilder<Patient> builder)
    {
        // .. noise

        builder.OwnsOne(p => p.PhoneNumbers, config =>
        {
            config.ToJson("phone_numbers");
        });

       // .. more noise
    }
    public void Configure(EntityTypeBuilder<Patient> builder)
    {
        // .. noise

        builder.OwnsOne(p => p.PhoneNumbers, config =>
        {
            config.ToJson("phone_numbers");
        });

       // .. more noise
    }


This all compiles and runs, but when I enter data into this column the value of the column is

select phone_numbers from patients
select phone_numbers from patients

{"Capacity": 4}
{"Capacity": 4}


I already tried

        builder.OwnsMany(p => p.PhoneNumbers, config =>
        {
            config.ToJson("phone_numbers");
            config.WithOwner(pn => pn.Patient);
        });
        builder.OwnsMany(p => p.PhoneNumbers, config =>
        {
            config.ToJson("phone_numbers");
            config.WithOwner(pn => pn.Patient);
        });

As @viceroypenguin | 🦋🐧 suggested, however this just gives a

Entity 'PhoneNumber' is mapped to JSON and also to a table or view 'phone_number', but its owner 'Patient' is mapped to a different table or view 'patients'. Every entity mapped to JSON must also map to the same table or view as its owner.

when initializing the database. Is this scenario still supported without a wrapper class, which in my opinion is quite an ugly workaround since this is quite the nice use case for a JSON column?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ EFCore NPGSQL: Date exception
C#CC# / help
3y ago
EFCore 8 - Owned type with navigation property not being included
C#CC# / help
3y ago
✅ EFCore type conversion.
C#CC# / help
3mo ago
✅ Working With Json
C#CC# / help
3y ago