简体   繁体   English

Linq Expression to NHibernate数据上下文中的SQL文本

[英]Linq Expression to SQL text within NHibernate data context

Assuming that I have setup NHibernate data context with all relations, tables, connection etc. 假设我已经用所有关系,表,连接等设置了NHibernate数据上下文。

How I would convert an instantiated .NET Expression (actually it's not a full SELECT but just a WHERE expression) to an SQL string? 我如何将实例化的.NET表达式(实际上不是完整的SELECT而是WHERE表达式)转换为SQL字符串?

I've seen many references. 我看过很多参考。 Like using ExpressionVisitor or building IQueryable Provider, but I believe NHibernate 3 has all these implementations and I just have to combine some classes to achieve the goal. 就像使用ExpressionVisitor或构建IQueryable Provider一样,但是我相信NHibernate 3具有所有这些实现,我只需要组合一些类即可实现目标。 Ant ideas? 蚂蚁的想法?

Well, right after posting this question an approach was found here: How can I have NHibernate only generate the SQL without executing it? 好吧,在发布此问题之后,就在这里找到了一种方法: 我如何让NHibernate仅生成SQL但不执行它? .

Actually it's possible to get a text only for the full SELECT query, otherwise the exception is raised: 实际上,可能只为完整的SELECT查询获取文本,否则会引发异常:

Cannot parse expression '(rec.Id == 6)' as it has an unsupported type. 无法解析表达式'(rec.Id == 6)',因为它的类型不受支持。 Only query sources (that is, expressions that implement IEnumerable) and query operators can be parsed. 只能解析查询源(即实现IEnumerable的表达式)和查询运算符。

查看有关如何通过编写自定义批处理程序来拦截sql查询的其他答案: NHibernate和拦截器-测量/监视SQL往返时间它也适用于Sql Server,一旦拦截了查询,您就可以自由地阻止其执行返回空结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM