Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use ARRAYs in Entity Framework + PostgreSql

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.

like image 529
ax1mx2 Avatar asked Mar 21 '15 18:03

ax1mx2


People also ask

What is Entity Framework for PostgreSQL?

Entity Framework is one of the most pervasive Object-Relational Mappers (ORMs) for ASP.NET. An ORM maps an application’s object entities to relational entities in a database, and allows developers to build and edit the database schema from the code. Furthermore, Entity Framework’s design makes it particularly friendly for PostgreSQL developers.

How to work with an array in PostgreSQL?

PostgreSQL provides various functions to work with an array. We can insert, update the array element by using an index. Insertion on the array element is very easy as we can use multiple syntaxes like [] operator or {} braces.

Do PostgreSQL arrays work with dotconnect?

Postgres arrays come in handy at times where you want to store a small or limited number of values into a single column. Probably depends on the data provider. Devart dotConnect claim they do.

Do PostgreSQL data types have commas?

Among the standard data types provided in the PostgreSQL distribution, all use a comma (, ), except for type box which uses a semicolon (; ). Each val is either a constant of the array element type, or a subarray. An example of an array constant is:


1 Answers

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; }
like image 188
Matt Williams Avatar answered Nov 15 '22 14:11

Matt Williams