EF Core 2.1中的全文本搜索?

cho*_*bo2 8 sql-server full-text-search entity-framework entity-framework-core

我正在使用全文搜索,但是关于如何使用EF Core 2.1进行搜索的问题还不是100%清楚。

似乎EF Core 2.1可能已实现了对全文搜索的部分支持,但我没有找到有关如何实际使用它的任何教程。

我的理解是,我将不得不向其中一列添加全文索引。

所以如果我有这张桌子

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();
        }

}
Run Code Online (Sandbox Code Playgroud)

如何将全文索引添加到我的Name属性?

Rya*_*ill 9

现在,您需要在迁移中使用SQL函数手动添加它们。

从EF Core 2.1开始,尚未建立全文索引的创建。有关更多详细信息,请参见此问题跟踪器https://github.com/aspnet/EntityFrameworkCore/issues/11488

综上所述;

在EF Core 2.1中,我们初步支持通过LINQ中的FreeText谓词进行全文本搜索,但这仅适用于已建立索引的数据库。EF Core和SQL Server提供程序没有提供任何配置模型的方法,因此迁移或确保创建的索引可以生成用于定义索引的正确SQL。

https://github.com/aspnet/EntityFrameworkCore/commit/2a6ccad8821f9360ae753bce41d63811185b8912上的测试中提取的FreeText示例C#Linq查询;

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);
}
Run Code Online (Sandbox Code Playgroud)