C
C#6mo 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 entity that can have a dynamic amount of phone numbers by making its property public List<PhoneNumber> PhoneNumbers { get; set; } = new(); a json column. According to the docs, in version 8 [ColumnType("jsonb")] has been deprecated in favor of using .ToJson(), which I'm now trying to apply since I just upgraded from net7 and Npgsql 7 to 8. I'm setting up 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 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} 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?
0 Replies
No replies yetBe the first to reply to this messageJoin