Repository navigation
Description
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:
- 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
#11295 (comment) 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.
Activity
divega commentedon Dec 9, 2015
@JocaPC We are of course busy completing more basic functionality than this at the moment but eventually I would love us to enable all of this. BTW, I think providing a way to declare indexes for values in the JSON payload (so that those queries can be optmized) would also be nice.
gdoron commentedon Feb 29, 2016
@rowanmiller
It would be helpful and user friendly if we could add to our POCO a dynamic property and through the modelbuilder mark it as JSON (
modelbuilder.Entity<Product>().Property(x=> x.CustomFields).AsJson()whereCustomFieldsisdynamic/ExpandoObject).And then we will be able to nicely query the dynamic object like:
Though I'm just not sure how indexing will work in SQL-Server 2016 for JSON columns @JocaPC
OData is using something similar with Open Types and it's intuitive and very easy to use.
omric-axonize commentedon Feb 29, 2016
OData Open Type story sounds like a perfect fit to SQL-Server 2016 + EF Core!
JocaPC commentedon Feb 29, 2016
You can add index on computed column that exposes JSON value:
Original queries don't need to be rewritten. When SQL Server finds a query that uses JSON_VALUE and if path in JSON_VALUE matches computed column that has index, it will use indexing. See
https://msdn.microsoft.com/en-us/library/mt612798.aspx
erichexter commentedon Apr 2, 2016
I would like to see this support.
DaveSlinn commentedon Apr 6, 2016
My team is currently investigating EF for a new application we are undertaking and I was hoping to use this very feature. If we were so inclined to invest in working on this feature, for our own selfish needs, would that be worthwhile contributing?
ikourfaln commentedon Jun 16, 2016
We realy need this functionality
ikourfaln commentedon Jun 16, 2016
At the moment, I think that we can just use FromSql function to execute raw SQL and include
JSON_VALUE.example:
soycabanillas commentedon Jul 31, 2016
My two cents (what I'm doing for now) for proposal 1:
I configure my entities this way:
x.Serialized is the JSON/text field.
y.Data is the object that is serialized into Serialized. Status is just a property of Data.
z.FStatus is the column where I'm going to store the value of y.Data.Status.
the heavyweight is done by the HasJson extension method. I include the code (not a clean one) for copy paste, but the important thing here is that I'm setting the FStatus property as a computed column, as @JocaPC commented.
The EntityMappingConfiguration thing is just a friendly way of having the configuration of an entity in its own class. You can see the implementation and the discussion here:
https://github.com/aspnet/EntityFramework/issues/2805#issuecomment-218548872
If anyone needs helps with this, just ask me.
This has several limitations, though, at least:
1 - The properties are computed columns. The value will not be set until the entity is saved. But it will be of use for indexing.
2 - It doesn't support polymorphism, at least not in an easy/clear way. You can't use the same data for different entities... or I haven't found the way:
a - You can't use different classes to point to the very same table #6001
b - You can't use different classes to point to the very same field #240
Sorry if something about my conclusions is not completely accurate. There are others more capable of validating them. I just tried to expose my experience so far.
soycabanillas commentedon Jul 31, 2016
And...
3 - This solution only supports SQL Server. No support for in memory database.
bjorn-ali-goransson commentedon Aug 2, 2016
I think the comment of @ikourfaln is superiour (and simplest to implement, ie already done) in combination with the flexible mapping idea.
ma3yta commentedon Sep 28, 2016
When it will be done?
ikourfaln commentedon Sep 29, 2016
@ma3yta 😞
not yet decided. must be moved from Backlog to a Milestone
106 remaining items
maumar commentedon Aug 23, 2022
initial support is now checked in: 4018482 and 0577a3a, closing this issue
MoazAlkharfan commentedon Sep 12, 2022
Will there be support for JsonSerializationOptions?
maumar commentedon Sep 26, 2022
@MoazAlkharfan eventually, we have #28815 to track this