【问题标题】:Entity Framework Core PostgreSQL hstore queriesEntity Framework Core PostgreSQL hstore 查询
【发布时间】:2020-05-22 03:58:06
【问题描述】:

我需要为 hstore 列中的值生成查询

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)

但是没有最好的办法,有什么办法可以实现这个查询翻译成这个sql?

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

【问题讨论】:

    标签: postgresql npgsql hstore ef-core-3.1


    【解决方案1】:

    Npgsql EFCore 提供程序目前不对 hstore 类型进行任何转换。这主要是因为扩展支持已经存在for the PostgreSQL jsonb type(包括你想做的事情),而且这种类型比 hstore 强大得多。考虑从 hstore 切换到 jsonb。

    This is the issue 跟踪 hstore 的查询翻译。

    【讨论】:

    • hstore 是完美的,因为我在选择表达式中使用,我需要选择一个键作为参数。示例 jsonb 在 where 表达式中。选择表达式中是否存在 jsonb 的翻译?
    • 您应该能够通过将using JsonDocument 映射到jsonb 来实现相同的效果。这种映射模式适用于键本身可以参数化的无模式场景。
    • @jrocha76:jsonb 可以做 hstore 可以做的所有事情,甚至更多。您只需使用 setting_value ->> 'key' 而不是 setting_value -> 'key'
    • 我试试 public JsonElement SettingValue { get;放; } 和 Select(b => new { b.SettingId, b.SettingParentId, SettingValue = b.SettingValue.GetProperty(lang) }),但是当值为 null 时 --> 抛出错误
    • @jrocha76 最好继续在stackoverflow.com/questions/61995777/… 中讨论这个问题,并留下这个关于 hstore 的问题。
    猜你喜欢
    • 2020-09-11
    • 2022-01-25
    • 1970-01-01
    • 2020-10-27
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多