You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This happens when we upgrade from dotnet version 8.0 to 9.0
We have a table column of type "double precision two dimensional array", when we add a record with an empty value, it will be recorded as '{}' in the database which can not be converted back from database to a two dimensional array and it fails with the exception in the title. It looks like it treats the empty values in the column as one dimensional array. If we make the column nullable then it works fine.
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
public class Vertex
{
public double X { get; set; }
public double Y { get; set; }
}
public class VertexConfig : IEntityTypeConfiguration<Vertex>
{
public void Configure(EntityTypeBuilder<Vertex> entity)
{
entity.Property(e => e.Vertex)
.HasColumnName("Vertex")
.HasConversion(new VertexesConverter());
}
}
public class VertexesConverter : ValueConverter<List<Vertex>, double[,]>
{
public VertexesConverter() : base(
v => ConvertToDatabase(v),
v => ConvertFromDatabase(v))
{
}
private static double[,] ConvertToDatabase(List<Vertex> Vertexes)
{
if (Vertexes == null || Vertexes.Count == 0)
{
return new double[0, 0]; // Empty 2D array
}
var array = new double[Vertexes.Count, 2];
for (int i = 0; i < Vertexes.Count; i++)
{
array[i, 0] = Vertexes[i].X;
array[i, 1] = Vertexes[i].Y;
}
return array;
}
private static List<Vertex> ConvertFromDatabase(double[,] data)
{
if (data.GetLength(0) == 0)
{
return new List<Vertex>(); // Handle empty 2D array
}
var Vertexes = new List<Vertex>();
for (int i = 0; i < data.GetLength(0); i++)
{
Vertexes.Add(new Vertex { X = data[i, 0], Y = data[i, 1] });
}
return Vertexes;
}
}
The text was updated successfully, but these errors were encountered:
dkezri
changed the title
Unable to cast object of type 'System.Double[]' to type 'System.Double[,]'
Unable to cast object of type 'System.Double[]' to type 'System.Double[,]' for empty values in 'double precision multi dimensional array'
Apr 1, 2025
This happens when we upgrade from dotnet version 8.0 to 9.0
We have a table column of type "double precision two dimensional array", when we add a record with an empty value, it will be recorded as '{}' in the database which can not be converted back from database to a two dimensional array and it fails with the exception in the title. It looks like it treats the empty values in the column as one dimensional array. If we make the column nullable then it works fine.
The text was updated successfully, but these errors were encountered: