Entity Framework 5, Code First, Full Text Search but IQueryable via CreateQuery?

旧巷老猫 提交于 2019-12-12 16:01:49

问题


I am using .NET 4.5 and EF 5 with Code First approach and now I need to implement Full Text Search. I have already read a lot about it and so far my conclusions are:

  • Stored procedures nor Table Value Functions can not be mapped with Code First.

  • Still I can call them using dynamic sql

    dbContext.Database.SqlQuery<Movie>(Sql, parameters)

But this returns IEnumerable and I want IQueryable so that I can do more filtering before fetching the data from db server. I know I can send those parameters to Db function but I don't want that.

  • What I have found that could fulfill my needs is CreateQuery function from IObjectContextAdapter that looks like this(Select All just for test):

    IQueryable<Movie> result = ((IObjectContextAdapter)dbContext).ObjectContext.CreateQuery<Movie>("SELECT * FROM Movie");

  • However executing this throws Exception" 'System.Data.EntitySqlException was unhandled HResult=-2146232006 Message=The query syntax is not valid. Near term '*', line 1, column 9.'

So the questions are:

  • Why do I get this exception and can it be fixed ?

  • If not is there any way with Code First to do FTS that returns IQueryable ?


回答1:


Try it like this

ObjectQuery<Movie> query = 
    objectContext.CreateQuery<Movie>(@"SELECT VALUE movie FROM Movies");  

As for why see these links

Differences from Transact-SQL Unlike Transact-SQL, Entity SQL does not support use of the * argument in the SELECT clause. Instead

Entity SQL Reference - SELECT
"SELECT VALUE" - value keyword in LINQ/Entity Framework query



来源:https://stackoverflow.com/questions/16134193/entity-framework-5-code-first-full-text-search-but-iqueryable-via-createquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!