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.

Map JSON values stored in database to EF properties

See original GitHub issue

SQL 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:

  1. 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}.
  2. 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

  1. 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.
  2. 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.
  3. 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:closed
  • Created 8 years ago
  • Reactions:657
  • Comments:104 (23 by maintainers)

github_iconTop GitHub Comments

49reactions
maumarcommented, Aug 23, 2022

initial support is now checked in: 401848251cd66f06fffe4baf8ad607f79ef60d05 and 0577a3acab690c9d884889d00e56f3743edb52c4, closing this issue

25reactions
sishuoyangcommented, Jan 29, 2020

I can’t believe this issue is still open in 2020! This feature is highly desirable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Columns in Entity Framework Core
In Entity Framework (EF) Core, JSON columns can be used to store and query JSON data in a database. · EF Core supports...
Read more >
How to store JSON in an entity field with EF Core?
The database is just being used to store parts of your domain. Instead you can use EF Core's HasConversion() method on the EntityTypeBuilder ......
Read more >
How to Store JSON in an Entity Field With EF Core
In this article, we are going to talk about how to store data in the JSON entity field using Entity Framework Core.
Read more >
Entity Framework Core 7 JSON Columns | Mapping
In this article, we are going to discuss about EF Core 7 feature that JSON data mapping, querying and updating operation.
Read more >
Announcing Entity Framework Core 7 RC2: JSON Columns
EF Core 7.0 (EF7) adds support for mapping aggregate types to JSON documents stored in “JSON columns” of a relational database. This allows ......
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