EF PostgreeSQL array_append

Viewed 80

I want append a item to column with type int[]. i see PostgreSQL document with array_append can do this. but how can we do this with EF Core? (i use Npgsql.EntityFrameworkCore.PostgreSQL)

2 Answers

This currently isn't supported (the list of supported array translations can be found here).

In the meantime, as a workaround you can use raw SQL queries to express your query directly in SQL, and possibly compose additional LINQ operators over that.

EDIT: opened https://github.com/npgsql/efcore.pg/issues/2026 to track, this should be doable for 6.0.

raw SQL Query:

var sql= $"UPDATE public.\"TableName\" SET \"ArrayColumnName\" = array_append(\"ArrayColumnName\", {value}) WHERE  ... ;";//fill ... with your condition

var result =await  _dbContext.Database.ExecuteSqlRawAsync(sql) ;//return int
Related