14

Is it possible to use arrays in Entity Framework with PostgreSql?

Suppose, for instance, we had the POCO class

        public class MyTable
        {
            [Key]
            [Column("gid")]
            public int Gid { get; set; }
            [Column("name")]
            public string Name { get; set; }
            [Column("email")]
            public string Email { get; set; }
            [Column("somedata")]
            public int[] SomeData { get; set; }
        }

At this point Entity Framework simply does not create the column "somedata" and skips it. Is there a way to do this anyway? And by that I mean not having to use a separate table. Postgres arrays come in handy at times where you want to store a small or limited number of values into a single column.

5
  • Probably depends on the data provider. Devart dotConnect claim they do. Commented Mar 21, 2015 at 19:20
  • 1
    @GertArnold, can you point to where Devart say they do this? I work on Npgsql and AFAIK EF itself isn't very open to types outside the basic ones. Commented Mar 22, 2015 at 14:13
  • Maybe I've said too much. Here they about their ADO.NET Provider: "It support a wide range of PostgreSQL-specific features, such as secure SSL connections, PostgreSQL notifications, PostgreSQL bulk data loading, GEOMETRY, PostgreSQL ARRAY types, and others." Commented Mar 22, 2015 at 15:34
  • Ah, OK. I would guess that means they support it at the ADO.NET layer, rather than in EntityFramework. The problem seems more to be with EF itself, and not with Npgsql's or dotConnect's implementation of it. But I haven't looked at this very deeply. Commented Mar 22, 2015 at 17:29
  • I don't believe that there is any support from EF for this, it was just not designed this way. Take a look at the following code. Commented Mar 22, 2015 at 19:48

1 Answer 1

15

It's possible to do this if you use Entity Framework Core with the Npgsql EF Core provider.

The code-first approach would be:

[Column("somedata", TypeName = "integer[]")]
public int[] SomeData { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.