【发布时间】:2021-12-04 02:20:49
【问题描述】:
我喜欢使用这个功能-this documentation 中提到的EF.Functions.TrigramsSimilarityDistance(s1, s2)。我正在尝试像这样调用这个函数-
string searchValue = "similar text";
var resultList = await _context.Comments.OrderBy(c => EF.Functions.TrigramsSimilarityDistance(c.Name, searchValue))
.ThenBy(t => t.Name)
.ToListAsync()
但是我看到了这个错误-
DbFunctions 不包含“TrigramsSimilarityDistance”的定义,并且没有可访问的扩展方法 TrigramsSimilarity Distance 接受类型为“DbFunctions”的第一个参数(您是否缺少 using 指令或程序集引用?)
我不确定如何导入 pg db 扩展。我所做的是-
using static Microsoft.Extensions.DependencyInjection.NpgsqlServiceCollectionExtensions;
但它不起作用。
更新-
我正在使用ASP.Net Core 3.1,所以我正在使用这两个 nuget 包-
然后进行配置,我在Startup.cs文件中做了这个-
services.AddDbContext<ApplicationDbContext>(options => {
options.UseNpgsql(Configuration.GetConnectionString("DevelopConnection"), option => option.UseTrigrams());
});
那么当我像上面提到的那样打电话时-
string searchValue = "similar text";
var resultList = await _context.Comments.OrderBy(c => EF.Functions.TrigramsSimilarityDistance(c.Name, searchValue))
.ThenBy(t => t.Name)
.ToListAsync();
得到这样的错误-
Npgsql.PostgresException (0x80004005): 42883: operator does not exist: text <-> text
Exception data:
Severity: ERROR
SqlState: 42883
MessageText: operator does not exist: text <-> text
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 473
File: d:\pginstaller_13.auto\postgres.windows-x64\src\backend\parser\parse_oper.c
Line: 731
Routine: op_error
谁能帮我解决这个问题?
【问题讨论】:
-
您使用的是哪个版本的 EF Core?您是否尝试过获取文档中提到的 nuget 包 Npgsql.EntityFrameworkCore.PostgreSQL.Trigrams? EF 找不到扩展方法,所以我猜某处缺少引用。
-
我已经安装了NuGet包,然后导入错误消失了,但仍然出现这个错误-
Exception thrown: 'System.InvalidOperationException' in Microsoft.EntityFrameworkCore.dll Exception thrown: 'System.InvalidOperationException' in StartupProject-Asp.NetCore-PostGRE.dll Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request. System.InvalidOperationException: The LINQ expression 'DbSet<Term> -
在您提供的文档中,您似乎需要在 DbContextOptionsBuilder 上调用扩展方法 UseTrigrams()。你在调用这个方法吗?
-
不,我必须这样称呼
-
确保在您的 PostgreSQL 服务器上激活了
pg_trgm扩展。
标签: c# postgresql entity-framework asp.net-core entity-framework-core