I have the query:
var query = DataContext.Fotos.Where(x => x.Pesquisa.Contais(\"myTerm\")
The SQL generated is:
SELECT
...
Seems that Entity Framework 6 does not support full text search, but there is a workaround with interceptors.
http://www.entityframework.info/Home/FullTextSearch
Update Link doesn't work so here is the original content:
Microsoft TSQL supports full-text query by means of predicates (CONTAINS and FREETEXT)
For example, you have table Notes
Create table Notes ( Id int Identity not null, NoteText text ) CREATE FULLTEXT CATALOG [Notes Data]
When you search this table for records containing word 'John', you need to issue
SELECT TOP (10) * from gps.NOTES WHERE contains(NoteText, '(john)')
Unfortunately, Enity framework does not support full-text search predicates still. For EFv6, you can make a workaround using interception.
The idea is to wrap search text with some magic word during inside plain String.Contains code and use interceptor to unwrap it right before sql is executed in SqlCommand.
To start, lets create the interceptor class:
public class FtsInterceptor : IDbCommandInterceptor { private const string FullTextPrefix = "-FTSPREFIX-"; public static string Fts(string search) { return string.Format("({0}{1})", FullTextPrefix, search); } public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext
interceptionContext) { } public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) { RewriteFullTextQuery(command); } public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext I used extension function In that can be defined like this:
static class LanguageExtensions { public static bool In
(this T source, params T[] list) { return (list as IList ).Contains(source); } } Now lets compose a sample how to use it. We need entity class Note:
public class Note { public int Id { get; set; } public string NoteText { get; set; } }
Mapping configuration for it:
public class NoteMap : EntityTypeConfiguration
{ public NoteMap() { // Primary Key HasKey(t => t.Id); } } And our DbContext ancestor:
public class MyContext : DbContext { static MyContext() { DbInterception.Add(new FtsInterceptor()); } public MyContext(string nameOrConnectionString) : base(nameOrConnectionString) { } public DbSet
Notes { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new NoteMap()); } } Now we ready to use it. Lets search for 'john':
class Program { static void Main(string[] args) { var s = FtsInterceptor.Fts("john"); using (var db = new MyContext("CONNSTRING")) { var q = db.Notes.Where(n => n.NoteText.Contains(s)); var result = q.Take(10).ToList(); } } }