Using SQL Server full-text indexing in the Entity Framework

Source: Internet
Author: User

Github:https://github.com/fissoft/fissoft.entityframework.fts

EntityFramework to use full-text indexing is a bit of a hassle, you need to use DbContext.Database.SqlQuery or execute to execute SQL directly. It is not possible to compile to check the error of reading, refactoring is not convenient.

However, EF6 adds interceptor and can perform both pre-and post-operation.

Eg:

 public class Ftsinterceptor:idbcommandinterceptor {#region interface        Impl public void nonqueryexecuting (DbCommand command, dbcommandinterceptioncontext<int> interceptioncontext) {} public void nonqueryexecuted (DbCommand command, dbcommandinterceptioncontext<int> Intercept Ioncontext) {} public void readerexecuting (DbCommand command, Dbcommandinterceptioncontext<dbdata Reader> interceptioncontext) {} public void readerexecuted (DbCommand command, Dbcommandintercepti  Oncontext<dbdatareader> interceptioncontext) {} public void scalarexecuting (DbCommand command, Dbcommandinterceptioncontext<object> interceptioncontext) {} public void Scalarexec uted (DbCommand command, dbcommandinterceptioncontext<object> interceptioncontext) {} #endregio n} 

Similar to filter or other AOP frameworks, there are 6 methods available, namely three query pre-and post-operation in Scalarexecute,nonqueryexecute,readerexecute

In general, the use of full-text indexing is to query the data, so the scalarexecute,readerexecute of these two queries need us to rewrite, the specific rewriting principle is that the DbCommand in the CommandText read out, It is then processed into a format that supports full-text indexing.

For details, refer to the GitHub code: Https://github.com/fissoft/Fissoft.EntityFramework.Fts

Use the following method as follows

1. Use nuget references, or download code compilation on GitHub

Install-package Fissoft.EntityFramework.Fts

2. Then, when the program starts, execute the following read sentence to add the interceptor

    Dbinterceptors.init ()

3. There are several ways you can use the query when executing

    Db. Tables.where (C=>c.fullname.contains (Fulltextsearchmodelutil.contains ("code"));    Db. Tables.where (C=>c.fullname.freetext (Fulltextsearchmodelutil.contains ("code EF")); Db. Tables.where (c=>"*". Contains (Fulltextsearchmodelutil.containsall ("code EF")); Db. Tables.where (c=>"*". Contains (Fulltextsearchmodelutil.freetextall ("code EF"         )); 

Using SQL Server full-text indexing in the Entity Framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.