Skip to content

Fully support multi-dimensional arrays #314

Description

@roji

Single-dimensional PostgreSQL arrays are well-supported, but not multi-dimensional ones. This issue is meant to see how many people actually want this - please upvote if you'd benefit from it.

Things that need to be implemented:

  • Literal value generation
  • ValueComparer (comparison and snapshotting)
  • PostgresArrayIndexExpression needs to have multiple indexes, just like .NET ArrayIndexExpression
  • Full querability as a primitive collection?

Activity

  1. added this to the Backlog milestone on Mar 8, 2018
  2. alexzaytsev-newsroomly commented on May 6, 2018

    @alexzaytsev-newsroomly

    @roji The documentation here: http://www.npgsql.org/efcore/mapping-and-translation.html says However, operations such as indexing the array, searching for elements in it, etc. aren't yet translated to SQL and will be evaluated client-side. This will probably be fixed in 1.2.
    Since the latest version is over 2.0, i wanted to check - is something like

    context.MyEntities.FirstOrDefault(x => x.ArrayProperty.Any(i => i == 42)) expected to be evaluated client side or actually sent to the backend?

    (i noticed that Contains() is listed as supported on the same page)

  3. roji commented on May 6, 2018

    @roji
    MemberAuthor

    Will fix the docs, thanks. Yes, array operation translation was added to 2.0, which means server-evaluation (see #120). The translation list on the docs page is correct, I'll remove the old sentence you spotted. If you run into any issues or client evaluation occurs, please open a new issue.

    Note, however, multidimensional arrays (this issue) aren't supported at all as well as single-dimensional ones.

  4. added a commit that references this issue on May 13, 2018
  5. 2 remaining items

  6. Rincho-M commented on Sep 5, 2023

    @Rincho-M

    Are there any plans to implement this feature, or is it the same as it was five years ago?

  7. roji commented on Sep 5, 2023

    @roji
    MemberAuthor

    @Rincho-M so far this hasn't received enough user feedback to warrant spending the time on it. What exact capability are you missing here?

  8. Rincho-M commented on Sep 6, 2023

    @Rincho-M

    @roji I want to write and read a model with a two-dimensional array property. It seems to save correctly, but it gives an index-out-of-range exception when I'm trying to read it

  9. roji commented on Sep 7, 2023

    @roji
    MemberAuthor

    @Rincho-M saving and loading is generally supposed to work: see the following minimal code sample which works for me with 7.0; if you're seeing something different, please open a new issue with a minimal repro. Anything beyond that may not work though.

    Code sample
    await ctx.Database.EnsureDeletedAsync();
    await ctx.Database.EnsureCreatedAsync();
    
    ctx.Blogs.Add(new() { Matrix = new[,] { { 1, 2 }, { 3, 4 } } });
    await ctx.SaveChangesAsync();
    
    ctx.ChangeTracker.Clear();
    
    var blog = await ctx.Blogs.SingleAsync();
    foreach (var i in blog.Matrix)
    {
        Console.WriteLine(i);
    }
    
    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseNpgsql("Host=localhost;Username=test;Password=test")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public int[,] Matrix { get; set; }
    }
  10. Rincho-M commented on Sep 7, 2023

    @Rincho-M

    @roji so I did some testing and it appears that it doesnt work only with char arrays. And not only with multidimensional arrays, but with one-dimentional ones too. I didnt find opened issue with this error so I will try to open one on this weekend

  11. MattParkerDev commented on Sep 23, 2024

    @MattParkerDev

    This appears to work with int[,] and double[,], at least with Postgres, but not enums, e.g. MyEnum[,].
    Given that enums are just integers, is there an easy way to support this?

  12. roji commented on Sep 23, 2024

    @roji
    MemberAuthor

    Enums need to be value-converted internally, and the value converter that takes care of arrays doesn't support multidimensional ones yet.

  13. added theissue type on Jun 14, 2025
  14. Pieeer1 commented on Oct 30, 2025

    @Pieeer1

    Is there a workaround outside of a custom json serialization? IE:

            way.Property(w => w.Nodes)
                .HasColumnName("nodes")
                .HasColumnType("jsonb")
                .HasConversion(
                    v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
                    v => JsonSerializer.Deserialize<long[][]>(v, (JsonSerializerOptions?)null)!
                );
  15. roji commented on Oct 31, 2025

    @roji
    MemberAuthor

    @Pieeer1 basic multidim array support is there - if all you want to do is serialize/deserialize (value converters aren't supported, possibly some other edge cases). So if all you want to do is serialize/deserialize (which is what your JSON value converter does), then you shouldn't need anything else.

    Note that PG has a proper array type, rather than serializing arrays to JSON.

  16. Pieeer1 commented on Oct 31, 2025

    @Pieeer1

    I was getting some issues with a bigint[][] with the data type set, I can retry without the custom column type and see what I get with the migration.

  17. Pieeer1 commented on Oct 31, 2025

    @Pieeer1

    I was getting some issues with a bigint[][] with the data type set, I can retry without the custom column type and see what I get with the migration.

    Well it would make sense why it wasn't liking it, apparently osm2pgsql stores the data in a bigint[][] but in reality it is actually just a singular dimensional big int array.

  18. DG4ever commented on Jun 15, 2026

    @DG4ever

    I was working with integer[,] for a while now. After updating to the latest ef core version I am getting „Unable to cast object of type 'System.Int32[]' to type 'System.Int32[,]'“ when querying empty arrays.
    Were there some breaking changes?

  19. roji commented on Jun 17, 2026

    @roji
    MemberAuthor

    Please open a new issue for that, with a minimal repro that clearly shows the regression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions