Skip to content

Unable to cast object of type 'System.Double[]' to type 'System.Double[,]' for empty values in 'double precision multi dimensional array' #3514

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

Open
dkezri opened this issue Mar 31, 2025 · 3 comments

Comments

@dkezri
Copy link

dkezri commented Mar 31, 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.

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;
    }
}



@dkezri 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
@vonzshik vonzshik transferred this issue from npgsql/npgsql Apr 1, 2025
@dkezri
Copy link
Author

dkezri commented Apr 7, 2025

Hi @vonzshik any progress on this issue?

@vonzshik
Copy link
Contributor

vonzshik commented Apr 7, 2025

I only work on Npgsql, so, uh, nothing from me.

@roji
Copy link
Member

roji commented Apr 7, 2025

It will unfortunately be a while before I can investigate this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants