简体   繁体   English

如何在EF核心2.1中使用FreeText

[英]How to use FreeText in EF core 2.1

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. 我看到Entity Framework核心2.1有一个使用FREETEXT的新功能,但我不知道如何使用它,因为没有我可以在网上找到的例子。

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

Has anyone used it yet and could give me a quick example? 有人用过它还能给我一个简单的例子吗?

First make sure you have the relevant packages installed Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer . 首先确保已安装相关软件包Microsoft.EntityFrameworkCoreMicrosoft.EntityFrameworkCore.SqlServer

Then ensure you have the following import: 然后确保您具有以下导入:

using Microsoft.EntityFrameworkCore;

Now you can use the FREETEXT SQL function like this: 现在您可以像这样使用FREETEXT SQL函数:

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 . 注意:您可以看到这是如何工作的单元测试, 例如

To create the full text index, there is currently no support for doing this automatically in Entity Framework Core. 要创建全文索引,目前不支持在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);

Note the 2nd parameter in the call to Sql to suppress transactions. 注意调用Sql的第二个参数来抑制事务。 If you omit that you may get an error stating: 如果省略,则可能会收到错误消息:

CREATE FULLTEXT CATALOG statement cannot be used inside a user transaction CREATE FULLTEXT CATALOG语句不能在用户事务中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM