使用 EF Core 和 Postgres 检查 bool 列的约束

问题描述 投票:0回答:1

我将 EF Core 与 Postgres 结合使用。我想创建一个“配置”表,其中仅一行,这意味着:

  • 添加具有默认值的位列
  • 添加检查约束以确保列具有该值
  • 在该列上添加唯一约束

所以我添加了一个像这样的阴影列:

builder.ToTable(x => x.HasCheckConstraint("CK_Configuration_IsSingleRow", "IsSingleRow = true"));
builder.Property<bool>("IsSingleRow").IsRequired();
builder.HasIndex("IsSingleRow").IsUnique();

但是我收到错误:

Npgsql.PostgresException:运算符不存在:布尔=整数
MessageText:运算符不存在:布尔 = 整数
提示:没有运算符与给定名称和参数类型匹配。

我尝试了

IsSingleRow = true
IsSingleRow = 'true'
IsSingleRow = 1
IsSingleRow = '1'

正确的语法是什么?

c# postgresql .net-core entity-framework-core npgsql
1个回答
0
投票

PostgreSQL 将未加引号的标识符折叠为小写;这意味着您需要在检查约束 SQL 中引用列名:

builder.ToTable(x => x.HasCheckConstraint("CK_Configuration_IsSingleRow", "\"IsSingleRow\" = true"));

您还应该能够省略

= true
,因为您的 IsSingleRow 列已经是布尔值。

© www.soinside.com 2019 - 2024. All rights reserved.