When using the EF SQLite found that the like statement can not be fully queried, looked at the resulting SQL statement similar to this
(CHARINDEX (@p__linq__2, [extent1].[ Leaguename])) > 0)
Check the information, in SQLite is not support charindex this function, in fact, the solution is very simple, we just have to implement a interceptor, and then replace the SQL statement, and then add to the EF can be, the following is the implementation of interceptor:
private static regex Replaceregex = new regex (@ "\ (\ () (. *?), (. *?) \) \) > 0\);p rivate 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; }
The first is to replace all charindex, and then to modify the value of the query. Then we add the interceptor to EF:
Public qiutandb (): Base ("Name=defaultconn") { dbinterception.add (new Sqliteinterceptor ()); }
EF SQLite's like statement, born into a charindex solution