Problem with nested type in jsonb column
See original GitHub issueAfter creating Db context like below:
public class MyDbContext<T> : DbContext
where T : class
{
private readonly string connectionString;
private readonly string table;
public MyDbContext(string connectionString, string table)
{
this.connectionString = connectionString;
this.table = table;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(connectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var entityTypeBuilder = modelBuilder.Entity<T>();
entityTypeBuilder.Property("Values").HasColumnType("jsonb");
entityTypeBuilder.ToTable(table, "public").HasNoKey();
}
}
and below row is in database:
-------------------------------------------------------------------------------------------------------------
| Id | Values |
-------------------------------------------------------------------------------------------------------------
| f92df955-50fc-4037-bd2f-4df1550a167f | {"_Item": {"a": 10}, "b": "bValue", "c": "cValue", "f": true} |
-------------------------------------------------------------------------------------------------------------
my classes is like below:
public class DbClass
{
public Guid Id { get; set; }
public OuterClass Values { get; set; }
}
public class OuterClass
{
public InnerClass _Item { get; set; }
public string b { get; set; }
public string c { get; set; }
public bool f { get; set; }
}
public class InnerClass
{
public int a { get; set; }
}
querying with dbContext throws below exception:
No coercion operator is defined between types 'System.String' and 'InnerClass'.
Am i doing something wrong?!
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Got column doesn't exist when query nested jsonb field in ...
The tax column has type jsonb . but I got this error if I select the nested feePercent field: select "uuid", "tax"- ...
Read more >How to query nested json strings in jsonb field in postgres?
I need to deserialize this string during the query and inspect its values. create table datas (id int, data jsonb); insert into datas...
Read more >JSON Fields for Nested Pydantic Models? #63
Creating a the object using the classes and writing them to the DB works as expected and writes the data as a dict...
Read more >Documentation: 15: 8.14. JSON Types
The jsonb data type supports array-style subscripting expressions to extract and modify elements. Nested values can be indicated by chaining subscripting ...
Read more >Dealing with nested JSON objects in PostgreSQL
A simpler way to do nested JSON attributes is to use the #> (Waffle Cone) operator with an array of the path you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@RZProf you always have the possibility of getting the entire JSON document out of the database, and then project out your Guid/TimeSpan in .NET (by using AsEnumerable/AsEnumerableAsync before the final projection). This will fetch more data than you actually need, but at the moment I can’t really dive deeper into it… EF Core 7 will probably have a significant focus around JSON, so this issue will probably go away, but most probably not for EF Core 6.
Thanks for the extra info. I’ll try to look into this at some point, in order to provide a fix that wouldn’t require the operator.