EF6: Full-Text Database Search First Approach

I found a link to make full-text search work through linq . However, the code seems to target the database first approach . How to make it work with database first approach ?

Relevant piece of code:

 public class NoteMap : EntityTypeConfiguration<Note> { public NoteMap() { // Primary Key HasKey(t => t.Id); } } public class MyContext : DbContext { static MyContext() { DbInterception.Add(new FtsInterceptor()); } public MyContext(string nameOrConnectionString) : base(nameOrConnectionString) { } public DbSet<Note> Notes { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new NoteMap()); } } 

As you can see above, the OnModelCreating function OnModelCreating called only in Code First Approach . I wonder what needs to be changed to make the code in the link work for the Database First approach.

+5
source share
3 answers

Well, I tried to implement the solution without OnModelCreating in general, and it turned out that it was not even needed to implement the FullText interceptor, as @Evk suggested.

+1
source

I propose a different approach. Create a table function with full-text search on the SQL server and call it from the Entity Framework with a parameter. A simplified example from my project, which is looking for the full text over two tables and can be easily called from EF:

 CREATE FUNCTION [dbo].[GetRealtyMapFulltext] (@criteria nvarchar(4000)) RETURNS TABLE AS RETURN (SELECT realty.Id AS realtyId, ( COALESCE(ftR.Rank,0) + COALESCE(ftObec.Rank,0)) AS FtRank FROM realty LEFT JOIN ruian_obec ON realty.obecId = ruian_obec.obec_kod Left JOIN CONTAINSTABLE(Realty, *, @criteria) ftR ON realty.Id = ftR.[Key] Left JOIN CONTAINSTABLE(ruian_obec, *, @criteria) ftObec ON realty.obecId = ftObec.[Key] AND ( COALESCE(ftR.Rank,0) + COALESCE(ftObec.Rank,0) > 0) 
+1
source

The database first generates all classes as partial, including the very convenient partial OnContextCreated method.

I cannot verify this at the moment, but you can try adding the MyContext_FTS.cs file with the following code:

 public partial class MyContext { partial void OnContextCreated() { DbInterception.Add(new FtsInterceptor()); } } 
+1
source

Source: https://habr.com/ru/post/1260037/


All Articles