Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

c# - 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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

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.

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

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 statement cannot be used inside a user transaction


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...