Map JSON values stored in database to EF properties
See original GitHub issueSQL Server, PostgreSQL, MySQL, and Oracle databases enable developers to store JSON in columns and use values from JSON documents in queries. As an example, we can create product table with few fixed columns and variable information stored as JSON:
Product[id,title,decription,datecreated,info]
info column can contain JSON text. Use cases are:
- I can put different non-standard information about products in JSON column as a set of key:value pairs in JSON column, e.g. {“color”:“red”,“price”:35.99,“status”:1}.
- I can have new kind of single table inheritance where I can put all properties specific to some leaf classes as a bag of key:values instead of additional columns.
In SQL Server/Oracle we can use JSON_VALUE function to read JSON values from JSN column by keys, e.g.:
SELECT id, title,
json_value(info,'$.price'),json_value(info,'$.color'),json_value(info,'$.status')
FROM product
PostgreSQL has ->> operator and MySQL has json_extract function that are similar.
Problem
Currently, JSON fields can be mapped to entities in entity framework as string properties and we can parse them using Json.Net library. It would be good if we could map properties in EF model to (JSON text+path).
Proposal
- Basic - Could we map property from EF model to string column that contains text and specify path of value in JSON (e.g. price, status) ? EF can extract json value on JSON path and populate property in EF model with proper type casting.
- Medium - Enable updates of properties mapped to json value. If some EF property that is mapped to JSON values is updated, EF could format all of them as JSON and save them back in the in JSON column.
- Advanced - Enable LINQ support over JSON properties. If we use Select(obj=>obj.price), or Where(obj => obj.price < 100) in LINQ queries, these predicates could be transformed to JSON_VALUE for SQL Server/Oracle, -> for PostgreSQL. This way we will be able to query JSON values directly from EF.
edited by @smitpatel on 16th March 2018
You can use JSON_VALUE
function using user defined function feature in EF Core 2.0
https://github.com/aspnet/EntityFrameworkCore/issues/11295#issuecomment-373852015 explains how to do it for SqlServer.
Some open questions/possible subtasks:
- Partial updates to JSON documents (would likely require modeling as owned entities)
- Allow configuring JsonSerializationOptions to customize JSON property name etc., on a property-by-property basis (but also allow some bulk configuration default?)
- Use or allow using the new System.Text.Json source generator introduced in .NET 6.0?
- Indexes should probably out of scope of general relational support, as support differs considerably across databases.
Issues
Issue Analytics
- State:
- Created 8 years ago
- Reactions:657
- Comments:104 (23 by maintainers)
Top GitHub Comments
initial support is now checked in: 401848251cd66f06fffe4baf8ad607f79ef60d05 and 0577a3acab690c9d884889d00e56f3743edb52c4, closing this issue
I can’t believe this issue is still open in 2020! This feature is highly desirable.