c# - EF Core 2.1 中的全文搜索?

标签 c# sql-server entity-framework entity-framework-core full-text-search

我正在考虑使用全文搜索,但对于如何使用 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/

相关文章:

sql-server - 为什么 FLOAT 给我的结果比 DECIMAL 更准确?

asp.net - EF 使用代码优先和一对多关系创建重复的外键

c# - 在 ASP.NET 网站中使用静态成员是不好的做法吗?

c# - 反射说接口(interface)方法在实现类型中是虚拟的,而实际上它们不是?

c# - 如果仅更改内部版本号或修订版号,则定位 GAC 程序集

c# - 如何在 Gridview 中进行数据绑定(bind)?

c# 将二进制数据读取到字符串中

c# - 如何在 C# 中从 sql server 运行代码

c# - Entity Framework - 使用长链依赖对象进行高效的预加载?

entity-framework - 可选 Entity Framework 复杂类型的必需属性