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.

Can't map Dictionary<string,object> (e.g. to JSON) since it's detected as a property bag

See original GitHub issue

With (PostgreSQL) native JSON support, it’s useful to map Dictionary directly to JSON columns. This works e.g. for Dictionary<string,string>, but fails for Dictionary<string,object> since the property is detected as a property bag:

public class Blog
{
    public int Id { get; set; }

    [Column(TypeName = "jsonb")]
    public Dictionary<string, object> JsonProperty { get; set; }
}

The exception:

The navigation 'Blog.JsonProperty' must be configured in 'OnModelCreating' with an explicit name for the target shared-type entity type, or excluded by calling 'EntityTypeBuilder.Ignore'.

We may want to stop detecting property bags if column has an explicit store type. Following on how type converters work, this can be worked around by configuring the property with .Metadata.SetProviderClrType(null).

/cc @AndriySvyryd

Originally filed by @ColinZeb in https://github.com/npgsql/efcore.pg/issues/2134

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:12
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Apr 22, 2023
1reaction
rojicommented, Nov 28, 2022

@Mfolmer you’re definitely not supposed to be able to map a Dictionary<string, object> via ToJson. At this point, ToJson is only for mapping strongly-typed CLR-based entity types. We have plans to improve on that (#28871), though support is likely to take the form of JsonDocument/JsonElement support rather than via Dictionary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Why can't property bag (Dictionary<string, object>) be ...
I have a direct reference to the document object in the loop... No you don't. You have the reference to the recipe of...
Read more >
Proper JSON and property bags - ample code - WordPress.com
I recently wrote a blog post where I argued that "JSON serialization" as commonly practiced in the software industry is much too ambitious....
Read more >
How to deserialize JSON to Dictionary<string, class> ...
I'm querying an API that returns a JSON dictionary in which the keys cannot be hard coded into a C# class before-hand. The...
Read more >
How to enable case-insensitive property name matching ...
Learn how to enable case-insensitive property name matching while serializing to and deserializing from JSON in .NET.
Read more >
Custom Dictionary JsonConverter using System.Text.Json
How to customize the deserialization/serialization of Dictionary using System.Text.Json and a custom JsonConverter.
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