我正在考虑使用全文搜索,但对于如何使用 EF Core 2.1 进行实现还不是 100% 清楚。
似乎 EF Core 2.1 可能已经实现了对全文搜索的部分支持,但我没有找到任何有关如何实际使用它的教程。
我的理解是,我必须向我的其中一列添加全文索引。
如果我有这张 table
public class Company {
public string Name {get; set;}
}
public class CompanyConfig : IEntityTypeConfiguration<Company>
{
public void Configure(EntityTypeBuilder<Company> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Name).HasMaxLength(100).IsRequired();
}
}
如何将全文索引添加到我的 Name 属性?
最佳答案
您现在需要在迁移中使用 SQL 函数手动添加它们。
从 EF Core 2.1 开始,尚未构建全文索引的创建。有关更多详细信息,请参阅此问题跟踪器 https://github.com/aspnet/EntityFrameworkCore/issues/11488 .
总结;
In EF Core 2.1 we have initial support for for full-text search via the FreeText predicate in LINQ, but this only works with databases that have already been indexed. EF Core and the SQL Server provider don't provide any way to configure the model so that migrations or EnsureCreated can generate the right SQL for defining the indexes.
FreeText 的 C# Linq 查询示例,从 https://github.com/aspnet/EntityFrameworkCore/commit/2a6ccad8821f9360ae753bce41d63811185b8912 的测试中提取;
using (var context = CreateContext())
{
var result = await context
.Employees
.Where(c => EF.Functions.FreeText(c.Title, "Representative"))
.ToListAsync();
Assert.Equal(result.First().EmployeeID, 1u);
Assert.Equal(
@"SELECT [c].[EmployeeID], [c].[City], [c].[Country], [c].[FirstName], [c].[ReportsTo], [c].[Title] FROM [Employees] AS [c] WHERE FREETEXT([c].[Title], N'Representative')",
Sql,
ignoreLineEndingDifferences: true,
ignoreWhiteSpaceDifferences: true);
}
关于c# - EF Core 2.1 中的全文搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51028387/