Closed JocaPC closed 2 years ago
It’s embarrassing how long they’ve taken. It’s been years now. Those who needed have already implemented their own workarounds by using function references.
Could you possibly explain how or know any links about this? I've been looking myself.
I have had both of the following approaches in production for years.
EF v6 ("OG") This article will get you most of the way. https://stackoverflow.com/a/50490674/459102
EF Core v2.2 It's a bit easier.
Add these to your DbContext (assumes the class name of your DbContext is SqlContext
):
public static string JsonValue(string column, [NotParameterized] string path)
{
throw new NotSupportedException();
}
public static string JsonQuery(string column, [NotParameterized] string path)
{
throw new NotSupportedException();
}
Then in OnModelCreating
map the functions
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDbFunction(typeof(SqlContext).GetMethod(nameof(JsonValue)))
.HasName("JSON_VALUE")
.HasSchema("");
modelBuilder.HasDbFunction(typeof(SqlContext).GetMethod(nameof(JsonQuery)))
.HasName("JSON_QUERY")
.HasSchema("");
initial support is now checked in: 401848251cd66f06fffe4baf8ad607f79ef60d05 and 0577a3acab690c9d884889d00e56f3743edb52c4, closing this issue
Will there be support for JsonSerializationOptions?
@MoazAlkharfan eventually, we have https://github.com/dotnet/efcore/issues/28815 to track this
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:
In SQL Server/Oracle we can use JSON_VALUE function to read JSON values from JSN column by keys, e.g.:
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
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:
Issues