-
Notifications
You must be signed in to change notification settings - Fork 3.3k
JSON: Allow weakly-typed mapping via Dictionary #29825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Duplicate of #29427 |
Re-opening, since dictionary mapping was not covered by "primitive collections". |
Link to the "issue" should be in the docs at least, was difficult to find.. |
@vchirikov the milestone generally expresses whether an issue is planned for a given release or not. |
I assume this would also meant support for |
@onionhammer that would be a combination of this and #28871. |
@roji putting querying aside for now, JsonElement can already be saved/serialized as an entity property, but Dictionary<string, JsonElement> cannot; |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@roji You wrote in this comment that mappings like I configured serialization and deserialization with modelBuilder.Entity<Result>(entity =>
{
entity.OwnsOne(x => x.OutputJSON, x =>
{
x.ToJson("output");
x.Property(y => y.Data)
.HasConversion(
v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
v => JsonSerializer.Deserialize<Dictionary<string, string?>>(v, (JsonSerializerOptions?)null)
);
});
}); However, when I perform
I even tried to perform dummy serialization and deserialization like this, but the exception is still the same: x.Property(y => y.Data)
.HasConversion(
v => "{}",
v => new Dictionary<string, string?>()
); Am I using value conversion wrong? |
@aleksvujic I'm a bit confused by the above - above you have an entity type Result, containing a JSON-mapped owned entity (OutputJSON), and inside that you want to have a If so, note that the entire weakly-typed dictionary (Data) would be serialized as a single string inside the larger JSON document. So you wouldn't get |
@roji I think you summarized it correctly. JSON in the {
"data": {
"john": "doe",
"hello": "world"
}
} The That's why I wanted to model // scaffolded DB model
[Table("results", Schema = "fit3")]
public partial class Result
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("output", TypeName = "jsonb")]
public string? Output { get; set; }
}
// custom class with additional strongly-typed OutputJSON property
public partial class Result
{
public Output? OutputJSON { get; set; }
}
// content of the "data" JSON field (dictionary)
public class Output
{
[JsonPropertyName("data")]
public Dictionary<string, string?>? Data { get; set; }
} If I understand correctly, this can't work - this exact problem is tracked by this issue. Correct? If I format the nested JSON document in the PostgreSQL table as string (shown below), then EF successfuly maps it to C# {"data": "{ \"john\": \"doe\", \"hello\": \"world\" }"} |
No. EF already supports mapping anything via value converters - whether it be a dictionary or something else. This issue tracks allowing mapping a Dictionary to JSON without value converters, which would mean that EF manages the conversion and knows what's going on; that would notably mean that you can e.g. use dictionary lookups inside queries, which is something that you can't do when value-converting a value. Now, there may be an EF bug specifically when nesting your value-converted dictionary within another JSON owned entity; but that wouldn't be related to this issue. Can you please open a new issue and include a minimal repro for the problem? |
#28871 tracks weakly-typed JSON mapping via JsonDocument/JsonElement, where the JSON document schema varies and so a strongly-typed model isn't appropriate.
We can also allow simply mapping arbitrary Dictionary types - this corresponds to how many people use
Dictionary<string, object>
, to map an entire hierarchy. See #26903 on this (currently seemed blocked by property bag detection).OwnsOne(x => x.Json, builder => { builder.ToJson(); })
where x.Json is a Dictionary.The text was updated successfully, but these errors were encountered: