title: Mapping to POCOs with top-level dictionary from Postgres jsonb column
body: <p>I have a .NET Core 3.1 application using EF Core and a Postgres database. In the database I have one jsonb column that I now want to map to a well-defined set of classes in EF Core.</p>
  
  <p>The content of the jsonb column looks like the following:</p>
  
  <pre><code>{
      "entry1": {
          "name": "entry1",
          "contents": {
              "entry1.1": {
                  "name": "entry1.1"
              },
              "entry1.2": {
                  "name": "entry1.2",
                  "contents": {
                      "entry1.2.1": {
                          "name": "entry1.2.1"
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>At the top level it is a dictionary mapping strings to entries. Each entry has a name and can have contents, which is again a dictionary mapping strings to entries.</p>
  
  <pre><code>public class Entry
  {
      public string name { get; set; }
      public Dictionary&lt;string, Entry&gt; contents { get; set; }
  }
  </code></pre>
  
  <p>The jsonb column itself is defined on a table like this:</p>
  
  <pre><code>public class MyTable {
      [Column(TypeName = "jsonb")]
      public Dictionary&lt;string, Entry&gt; Entries { get; set; }
  }
  </code></pre>
  
  <p>The problem with this now is that it simply doesn't work. When I fetch an entry from the database with EF Core, the "Entries" property does contain indeed a dictionary with a single key "entry1", but the value of that key is an empty <code>Entry</code> object (name and contents are both null).</p>
  
  <p>The <a href="https://www.npgsql.org/efcore/mapping/json.html?tabs=data-annotations%2Cpoco" rel="nofollow noreferrer">Npgsql documentation on mapping jsonb columns to POCOs</a> doesn't explain how to handle dictionaries in this case. I couldn't find any examples with a top-level dictionary in the jsonb column, so I'm not entirely sure I'm doing this right.</p>
  
  <p>How can I wire this up correctly so that my jsonb column gets mapped to a dictionary of Entry objects?</p>