Search code examples
postgresqlnpgsqlhstoreef-core-3.1

Entity Framework Core PostgreSQL hstore queries


I need generate query for value in hstore column

var collection = await _context.Settings
.Select(b => new
{
    SettingId = b.SettingId,
    SettingParentId = b.SettingParentId,
    SettingValue = (b.SettingValue.ContainsKey('key') ? b.SettingValue['key'] : "")
})
.OrderBy(x => x.SettingId)

But is no best way, exist any way to realize this query to translate to this sql?

SELECT setting_id, 
       setting_parent_id,
       setting_value -> 'key' AS setting_value
FROM settings

Solution

  • The Npgsql EFCore provider doesn't currently do any translations for the hstore type. This is mainly because extended support is already present for the PostgreSQL jsonb type (including what you want to do), and that type is much more powerful than hstore. Consider switching from hstore to jsonb.

    This is the issue tracking query translation for hstore.