Open Supernectar opened 1 year ago
@Supernectar JObject
is supported as an implementation detail and may not be supported in future releases. What is your motivation for using JArray
or JObject
properties in your entity types?
@ajcvickers By excluding support for JArray
or JObject
, we are essentially limiting the flexibility for users to store objects containing custom fields. This limitation forces developers to create a separate model for each potential body input the user might provide, which is clearly impractical and not a viable solution.
Duplicate of #28871 and/or #29825.
The current support generally indeed requires a strongly-typed model. We do have plans to allow for weakly-typed/dynamic models as you'd like. However, note that if that's done via JArray/JObject, that ties you to the specific JSON package used by the provider - that's currently Newtonsoft.Json but should switch to System.Text.Json at some point (and that would be a breaking change). Because of this, simply allow non-JSON .NET Dictionary/List is probably a better approach here.
File a bug
When using the
Microsoft.EntityFrameworkCore.Cosmos
database provider, there is an issue with setting a model property's type as JArray. Attempting to do so results in the following exception being thrown:Include your code
To reproduce this bug, you can clone this repo https://github.com/Supernectar/EFCosmosJArrayBug.git. Simply building and running the project should trigger the exception mentioned above.
As a workaround, modifiying MyDbContext.cs to include a ValueConverter stops the exception from being thrown
However, this is not the expected solution, since this way data is stored as a stringified json instead of an actual json array. Cosmosdb supports storing actual json objects/arrays.
Also changing the object type to JObject doesn't throw the error, and I'm able to save it properly as a json object in cosmosdb, which leads me to think that it is a supported datatype. So why is the JObject supported and not the JArray, although they come from the same library?
Include stack traces
Include provider and version information
EF Core version: 7.0.9 Database provider: Microsoft.EntityFrameworkCore.Cosmos Target framework: .NET 6.0 Operating system: Windows 11 IDE: Visual Studio 2022 17.6.5