Entity Framework 中使用SQL Server全文索引(Full Text Search)

來源:互聯網
上載者:User

標籤:

GitHub:https://github.com/fissoft/Fissoft.EntityFramework.Fts

 

EntityFramework中原來使用全文索引有些麻煩,需要使用DbContext.Database.SqlQuery或Execute去直接執行SQL。那樣不能靠編譯來檢查讀法錯誤,重構也不方便。

不過EF6增加Interceptor,可以執行前置和後置操作。

Eg:

 public class FtsInterceptor : IDbCommandInterceptor    {        #region interface impl        public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)        {        }        public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)        {        }        public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)        {        }        public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)        {        }        public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)        {                  }        public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)        {        }        #endregion}

  與Filter或其它Aop架構差不多,這裡提供了一共6個方法,分別是在ScalarExecute,NonQueryExecute,ReaderExecute 這三種查詢 前置及後置操作

一般來說,使用全文索引是為了查詢資料,所以ScalarExecute,ReaderExecute這兩種查詢的前置方法需要我們改寫,具體改寫的原理是,將DbCommand中的CommandText讀取出來,然後處理成支援全文索引的格式。

細節請參考GitHub的代碼:https://github.com/fissoft/Fissoft.EntityFramework.Fts

使用時按以下方法即可

1.通過Nuget引用,或下載GitHub上的代碼編譯

PM> Install-Package Fissoft.EntityFramework.Fts

2.然後在程式啟動時執行以下讀句添加攔截器

    DbInterceptors.Init()

3.執行查詢時可以使用以下幾種方法

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

Entity Framework 中使用SQL Server全文索引(Full Text Search)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.