The Like Statement of EF SQLite generates a CHARINDEX solution, sqlitecharindex

Source: Internet
Author: User

The Like Statement of EF SQLite generates a CHARINDEX solution, sqlitecharindex

When EF SQLite is used, it is found that the Like statement cannot be completely queried. The generated SQL statement is similar to this

(CHARINDEX(@p__linq__2, [Extent1].[LeagueName])) > 0)

After checking the information, SQLite does not support the CHARINDEX function. In fact, the solution is very simple. We only need to implement an Interceptor and replace the SQL statement, then you can add it to EF. The following is the implementation of Interceptor:

Private static Regex replaceRegex = new Regex (@ "\ (CHARINDEX \((.*?), (.*?) \)> 0 \) "); private void ReplaceCharIndexFunc (DbCommand command) {var flag = false; var text = replaceRegex. replace (command. commandText, m => {if (! M. success) return m. value; flag = true; var key = m. groups [1]. value; var name = m. groups [2]. value; // Replace the parameter foreach (DbParameter commandParameter in command. parameters) {if (commandParameter. parameterName = key. substring (1) {commandParameter. value = $ "% {commandParameter. value }%"; break ;}}return $ "{name} LIKE {key}" ;}); if (flag) command. commandText = text ;}

First, replace all charindexes with regular expressions, and then modify the query value. Then we add Interceptor in EF:

public QiuTanDb() : base("name=defaultConn")        {            DbInterception.Add(new SqliteInterceptor());        }

  

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.