question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with nested type in jsonb column

See original GitHub issue

After 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:open
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Sep 6, 2021

@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.

1reaction
rojicommented, Jul 17, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found