9

I have the following query:

SELECT Animals.name 
FROM Animals 
WHERE CONTAINS(*, 'feline AND black');

I am having trouble converting it to an Entity Framework Core query. I have a SQL Server with a catalog that has a few indexes.

I want to be able to use FREETEXT and CONTAINS to do a fulltext query on the tables. I cannot find the method in Entity Framework Core for fulltext search with CONTAINS.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Daniel
  • 343
  • 1
  • 4
  • 7
  • As far as I know, EF Core does *not* support fulltext searching in SQL Server natively. You might need to package those queries up into stored procedure, which you can call from EF Core – marc_s Oct 05 '18 at 20:33
  • 1
    Have you looked at https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.sqlserverdbfunctionsextensions.freetext?view=efcore-2.1 – IEnjoyEatingVegetables Oct 05 '18 at 20:46
  • I did look into that. Unfortunately, they don't have the method "CONTAINS" that can be used to use logical operators. So far it is looking like EF Core does not support fulltext searching. – Daniel Oct 05 '18 at 20:55
  • I think the only way to get around this is just to perform raw sql queries with Entity Framework. https://learn.microsoft.com/en-us/ef/core/querying/raw-sql – Daniel Oct 05 '18 at 20:56

2 Answers2

19

This is possible as of EF Core 2.1. You have to add a using statement for Microsoft.EntityFrameworkCore but after that you can use it as shown below

var results = db.Widgets
    .Where(x => EF.Functions.FreeText(x.ColumnName, "search text"));
runxc1 Bret Ferrier
  • 8,096
  • 14
  • 61
  • 100
0

Yes it is posible using EF 6, just use EF.Functions.Contains

var results = db.Widgets
    .Where(x => EF.Functions.Contains(x.ColumnName, "search text"));

Important "search text" do not allow blank spaces you have to separate each word using logic operators like AND OR, for example if you want to search records with "green" and "blue", you have to put "green AND blue", not "green blue"