如何在EF core 2.1中使用FreeText [英] How to use FreeText in EF core 2.1

查看:164
本文介绍了如何在EF core 2.1中使用FreeText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Entity Framework core 2.1具有使用 FREETEXT 的新功能,但是我不确定如何使用它,因为没有可以在网上找到的示例。

I see that Entity Framework core 2.1 has a new feature to use FREETEXT, but I am not sure how to use it as there are no examples that I can find online.

https://github.com / aspnet / EntityFrameworkCore / issues / 11484

有人使用过它,可以给我一个简单的例子吗?

Has anyone used it yet and could give me a quick example?

推荐答案

首先请确保已安装相关的软件包 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore .SqlServer

First make sure you have the relevant packages installed Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer.

然后确保您具有以下导入:

Then ensure you have the following import:

using Microsoft.EntityFrameworkCore;

现在,您可以使用 FREETEXT SQL函数像这样:

Now you can use the FREETEXT SQL function like this:

var results = context.Foos
    .Where(f => EF.Functions.FreeText(f.ColumnName, "search text"));

注意:您可以在单元测试中看到它的工作方式,例如

Note: You can see how this works in the unit tests, for example.

要创建全文索引,当前在Entity Framework Core中不支持自动执行此操作。相反,您需要手动将代码添加到迁移中。因此,像平常一样创建一个迁移,打开它并添加类似于以下内容的行:

To create the full text index, there is currently no support for doing this automatically in Entity Framework Core. Instead, you need to manually add the code to the migration. So, create a migration as you normally do, open it up and add lines similar to this:

Sql("CREATE FULLTEXT CATALOG ft AS DEFAULT", true);
Sql("CREATE FULLTEXT INDEX ON dbo.TableName(ColumnName) KEY INDEX UI_TableName_ColumnName WITH STOPLIST = SYSTEM", true);

请注意对 Sql 抑制交易。如果您忽略了可能会指出以下错误:

Note the 2nd parameter in the call to Sql to suppress transactions. If you omit that you may get an error stating:


CREATE FULLTEXT CATALOG语句不能在用户事务内使用

CREATE FULLTEXT CATALOG statement cannot be used inside a user transaction

这篇关于如何在EF core 2.1中使用FreeText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆