C# 在postgresql/npgsql+;埃夫科尔

C# 在postgresql/npgsql+;埃夫科尔,c#,postgresql,entity-framework-core,npgsql,jsonb,C#,Postgresql,Entity Framework Core,Npgsql,Jsonb,在EFCore+npgsql中,我想使用postgresql 10创建一个tsvector列,以允许对模型中的某些字段进行全文搜索: 公共类MyModel { 公共字符串标题{get;set;} 公共字符串说明{get;set;} [列(TypeName=“jsonb”)] 公共字符串JSON{get;set;} } 我希望全文搜索包括来自JSONB列的值,以便根据我的更新模型,如下所示: 公共类MyModel { 公共字符串标题{get;set;} 公共字符串说明{get;set;} [列(

在EFCore+npgsql中,我想使用postgresql 10创建一个tsvector列,以允许对模型中的某些字段进行全文搜索:

公共类MyModel
{
公共字符串标题{get;set;}
公共字符串说明{get;set;}
[列(TypeName=“jsonb”)]
公共字符串JSON{get;set;}
}
我希望全文搜索包括来自JSONB列的值,以便根据我的更新模型,如下所示:

公共类MyModel
{
公共字符串标题{get;set;}
公共字符串说明{get;set;}
[列(TypeName=“jsonb”)]
公共字符串JSON{get;set;}
公共NpgsqlTsVector SearchVector{get;set;}
}
在my DB上下文中的
OnModelCreating
中添加以下索引:

modelBuilder.Entity(m=>
{
//创建一些其他索引
m、 HasIndex(e=>new{e.SearchVector});
});
然后创建迁移和编辑(根据文档)以获得以下方法:

protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.AddColumn(
名称:“搜索向量”,
表:“MyModels”,
可为空:真);
migrationBuilder.CreateIndex(
名称:“IX_MyModels_SearchVector”,
表:“MyModels”,
专栏:“搜索向量”)
注释(“Npgsql:IndexMethod”、“GIN”);
migrationBuilder.Sql(
@“在插入或更新之前创建触发器我的模型搜索向量更新”
在“MyModels”上为每行执行过程
tsvector_update_触发器(““SearchVector”、“pg_catalog.english”、“Title”、“Description”、“JSON”);”;
//我正在更新现有表,因此:
Sql(“更新\“MyModels\”集\“Title\”=“Title\”;”);
}
受保护的覆盖无效关闭(MigrationBuilder MigrationBuilder)
{
migrationBuilder.DropIndex(
名称:“IX_MyModels_SearchVector”,
表:“MyModels”);
migrationBuilder.DropColumn(
名称:“搜索向量”,
表:“MyModels”);
Sql(“DROP TRIGGER my_model_search_vector_update”);
}
但在所有这些之后,当应用更新数据库时,我看到:

Failed executing DbCommand (50ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
UPDATE "MyModels" SET "Title" = "Title";
Npgsql.PostgresException: column "JSON" is not of a character type

我假设这是因为JSONB列是二进制数据。有可能实现我所追求的目标吗?我对postgresql、npgsql和EFCore比较陌生。

我很确定您不能在postgresql
jsonb上进行全文搜索,因为正如您所写,它不是文本类型。使用
json
会有一个小小的变化——这就是文本存储——但这种类型更接近于简单的
text
,并且没有提供二进制
jsonb
的许多优点

然而,取决于您想要做什么,
jsonb
有相当多的搜索功能(PostgreSQL 12中也有SQL/JSON JSONPATH)。此功能当前未映射到EF Core中,但这不应阻止您通过原始SQL使用它