实体框架5,代码第一,全文搜索,但可以通过CreateQuery? [英] Entity Framework 5, Code First, Full Text Search but IQueriable via CreateQuery?

查看:146
本文介绍了实体框架5,代码第一,全文搜索,但可以通过CreateQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET 4.5和EF 5与Code First方法,现在我需要实现全文搜索。
我已经读了很多,到目前为止,我的结论是:




  • 存储过程和表值函数不能使用代码优先映射。


  • 仍然可以使用动态sql来调用它们



    dbContext.Database.SqlQuery< Movie>(Sql,参数)




但是这返回IEnumerable我想要IQueryable,以便在从数据库服务器获取数据之前可以进行更多的过滤。我知道我可以把这些参数发送给Db函数,但是我不想这样做。




  • 我的需求是来自IObjectContextAdapter的CreateQuery函数,看起来像这样(选择全部只是为了测试):



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


  • 异常
    'System.Data.EntitySqlException未处理
    HResult = -2146232006
    消息=查询语法无效近期'*',第1行第9列' p>




所以问题是:




  • 为什么我得到这个例外,可以修复?


  • 如果没有任何方式与Code First做FTS返回IQueryable ?



解决方案

尝试这样

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

至于为什么看到这些链接


与Transact-SQL的区别与Transact-SQL不同,实体SQL不
sup端口在SELECT子句中使用*参数。相反


实体SQL参考 - SELECT

SELECT VALUE - LINQ / Entity Framework查询中的value关键字


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 ?

解决方案

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

这篇关于实体框架5,代码第一,全文搜索,但可以通过CreateQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆