-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Support Jagged and Multidimensional Arrays (Cosmos DB) #26824
Comments
While jagged arrays should be OK, does JSON (and therefore CosmosDB) support multidimensional arrays? I'm not too sure how that would work. We could certainly serialize a .NET multidimensional array to a JSON jagged array, but then deserialization would yield a .NET jagged array. |
JSON doesn't support multidimensional arrays as such. Both Multi and Jagged would serialize to the same JSON format: [ [ ], [ ] ] Would it be possible to deserialize based on the intended receiving type? As an example, deserializing the same 2D JSON string array works for both types using JsonConvert: var arrayString = "[ [\"mystring1-1\", \"mystring1-2\"], [\"mystring2-1\", \"mystring2-2\"] ]";
var test = JsonConvert.DeserializeObject<string[][]>(arrayString);
log.LogInformation(test.ToString()); // System.String[][]
log.LogInformation(test[0][0]); // mystring1-1
var test2 = JsonConvert.DeserializeObject<string[,]>(arrayString);
log.LogInformation(test2.ToString()); // System.String[,]
log.LogInformation(test2[1,1]); // mystring2-2 |
That may indeed be possible. |
/cc @AndriySvyryd |
I have a similar case, with a small difference about exception. My table has a json column, which is like: [{"option": ["as", "around"], "answer": ["4", "0"], "position": [[16, 6], [23, 2]]}] And the code model is like: public class MyTableEntity
{
public List<QuestionContent> Value { get; set; }
}
public sealed class QuestionContent
{
public string[] Option { get; set; }
public string[] Answer { get; set; }
public int[][] Position { get; set; }
} When I want to use JSON column support in EF Core 8 like the following, entity.OwnsMany(e => e.Value, ob =>
{
ob.ToJson();
ob.Property(c => c.Option)
.HasJsonPropertyName("option");
ob.Property(c => c.Answer)
.HasJsonPropertyName("answer");
ob.Property(c => c.Position)
.HasJsonPropertyName("position")
.HasConversion(
v => JsonSerializer.Serialize(v, JsonSerializerOptions.Default),
v => JsonSerializer.Deserialize<int[][]>(v, JsonSerializerOptions.Default));
}); an exception is thrown as,
If I comment out the last And if I ignore the |
I've just started working with the entity framework and am very pleased to find a solid ORM in C# land. I have an Azure Function app set up with the Cosmos DB provider. While many types are supported out of the box, I encountered an issue when using Jagged and Multidimensional Arrays (either [][] or [,] respectively).
I am currently using the
6.0.0
version ofMicrosoft.EntityFrameworkCore.Cosmos
in anet6.0
framework app.My current solution is to serialize the array into a string on store and deserialize on retrieval. This is done like so in the model builder:
I did try making an OwnsMany relationship just to see if that worked but it throws a different error:
In the context of Cosmos, Jagged and Multidimensional arrays should ideally be supported by default as the data structures in JSON already support this (both would render out to the same JSON and could be converted back to their original respective type). Quick example of json serialization for both types (for thoroughness):
Presumably, this could be extended to 3D and 4D multidimensional arrays as well but my case only needs 2d.
I would love to see these data types handled automatically instead of requiring conversion to a string in a future release of the Entity Framework Cosmos DB provider. Thank you for your time and consideration!
The text was updated successfully, but these errors were encountered: