-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Map JSON values stored in database to EF properties #4021
Comments
@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. |
@rowanmiller 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. |
OData Open Type story sounds like a perfect fit to SQL-Server 2016 + EF Core! |
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 |
I would like to see this support. |
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? |
We realy need this functionality |
At the moment, I think that we can just use FromSql function to execute raw SQL and include var contacts = _context.Contacts.FromSql("SELECT Id, Name, Address, City, State, Zip " +
"FROM Contacts " +
"WHERE JSON_VALUE(Info, '$.Moniker') = @p1", moniker1); |
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. 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: If anyone needs helps with this, just ask me. This has several limitations, though, at least: a - You can't use different classes to point to the very same table #6001 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. |
And... 3 - This solution only supports SQL Server. No support for in memory database. |
I think the comment of @ikourfaln is superiour (and simplest to implement, ie already done) in combination with the flexible mapping idea. |
When it will be done? |
@ma3yta 😞 |
I find the implementation complexity for this feature, compared with its 2016-09-29 16:48 GMT+02:00 IKOURFALN Slimane notifications@github.com:
|
I also think this would be very interesting to have JSON query support in EF Core. |
Actually, for me, LINQ support would be secondary (or ternary) to the actual mapping functionality. Nice, of course, but if it takes much more time the "milestones" should be split up. IMO ... LINQ (as in: the query expression parsing bit) is a leaky abstraction that's not all that useful. It's often quite confusing. The IEnumerable extensions are stellar, though, the all-time best idea that the .NET team has conceived of. But I digress. |
Is it possible to work around this limitation in todays codebase? For example, I'd like a simple integer to be mapped to a Link object that contains an ID ... |
@bjorn-ali-goransson it's not natively supported but with Field Mapping support (added in 1.1 - currently in preview) you could map the integer to a private field and then have a property that converts to/from the Link object. See this post for details on 1.1 Preview1 and how to use field mapping https://blogs.msdn.microsoft.com/dotnet/2016/10/25/announcing-entity-framework-core-1-1-preview-1/. |
Why just not to start from simpler things: smartly serialize entity (with all its "navigation") to JSON using model information to avoid circular references? Together you will be able just to insert JSON DB fields "as is". Then user will be able easy to send the serialized result to further layers (usually SPA). It should cover 95% of JSON DB field usages. |
Yes, agree that we want mapping for just selection. It's not critical to serialize/deserialize on update but would be very helpful with odata sorting and filtering. |
Good to have something like UserType in NHibernate http://blog.denouter.net/2015/03/json-serialized-object-in-nhibernate.html?m=1 |
@rowanmiller maybe this helps to implement json support https://www.linkedin.com/pulse/quering-json-using-hibernate-marvin-froeder |
@rowanmiller @divega Maybe JSON mapping could be implemented using backing fields. Here is a proposal: #7394 |
How is this feature tracking for the EF7 release? |
7 or 8 more years. Just be patient. |
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. |
Well... we DID have a migration to Postgres scheduled for this year so we can take advantage of the Postgres provider's implementation of this, but now with the announcement of its standardization we figured we'll wait for it instead. Of course, that is, unless it doesn't drop this year. Which brings me back to my question.... |
I doubt you will get any type of commitment at this stage. Keep an eye on Plan for Entity Framework Core 7.0 and 7.0.0 milestone and hope it stays in - #13947 was recently punted. |
There's a draft PR with initial work #28171 |
That's promising, but a draft PR #24909 with initial work for value conversions was subsquently punted. Hopefully JSON columns makes it into preview 7 or 8, otherwise it's cutting it close for the November release. |
Switch to Postgres (if you can) and don’t look back. |
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") EF Core v2.2 Add these to your DbContext (assumes the class name of your DbContext is
Then in
|
Will there be support for JsonSerializationOptions? |
@MoazAlkharfan eventually, we have #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#11295 (comment) explains how to do it for SqlServer.
Some open questions/possible subtasks:
Issues
The text was updated successfully, but these errors were encountered: