-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
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 of Microsoft.EntityFrameworkCore.Cosmos in a net6.0 framework app.
System.Private.CoreLib: Exception while executing function: MyFunc. Microsoft.EntityFrameworkCore: The property 'MyProperty' is of type 'MyType[][]' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
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:
class MyType { }
class MyParentType
{
MyType[][] MyProperty { get; set; }
}
// Within the OnModelCreating function
modelBuilder.Entity<MyParentType>()
.Property(p => p.MyProperty).HasConversion(
g => JsonConvert.SerializeObject(g),
g => JsonConvert.DeserializeObject<MyType[][]>(g)
);I did try making an OwnsMany relationship just to see if that worked but it throws a different error:
// Within the OnModelCreating function
modelBuilder.Entity<MyParentType>()
.OwnsMany(p => p.MyProperty);System.Private.CoreLib: Exception while executing function: MyFunc. Microsoft.EntityFrameworkCore: The specified type 'MyType[]' must be a non-interface reference type to be used as an entity type.
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):
var test = new string[2][];
test[0] = new string[2] { "my string test1-1", "my string test1-2" };
test[1] = new string[2] { "my string test2-1", "my string test2-2" };
log.LogInformation(JsonConvert.SerializeObject(test));
// [["my string test1-1","my string test1-2"],["my string test2-1","my string test2-2"]]
var test2 = new string[2,2]
{
{ "my second test1-1", "my second test1-2" },
{ "my second test2-1", "my second test2-2" }
};
log.LogInformation(JsonConvert.SerializeObject(test2));
// [["my string test1-1","my string test1-2"],["my string test2-1","my string test2-2"]]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!