简体   繁体   English

当CacheMode = Ignore时,为什么NHibernate.ISession.CreateQuery返回与CreateSQLQuery不同的东西?

[英]Why would NHibernate.ISession.CreateQuery return something different from CreateSQLQuery when CacheMode=Ignore?

This is a pretty simple question but I'm baffled by it. 这是一个非常简单的问题,但我对此感到困惑。 In the immediate window of Visual Studio, I see this: 在Visual Studio的即时窗口中,我看到以下内容:

session.CacheMode
Ignore

session.CreateQuery("SELECT COUNT(*) FROM Protein").List()
Count = 1
    [0]: 0

session.CreateSQLQuery("SELECT COUNT(*) FROM Protein").List()
Count = 1
    [0]: 6

There could be several causes for something like this, check your mappings for the following: 可能有多种原因导致这种情况,请检查以下映射:

  • A <class> mapping with a where filter. 具有where过滤器的<class>映射。
  • A <discriminator> with force="true" attribute. 具有force="true"属性的<discriminator>
  • A <join> mapping that doesn't match on one side and isn't specified as optional="true" . 一侧不匹配且未指定为optional="true"<join>映射。
  • A custom <loader query-ref="..."> for the entity. 实体的自定义<loader query-ref="...">
  • A <filter> on the entity that has been enabled through session.EnableFilter . 通过session.EnableFilter启用的实体上的<filter>
  • The class Protein isn't mapped to the Protein table. Protein类未映射到Protein表。
  • You could have 2 tables named Protein in different schemas, so NHibernate is querying foo.Protein but your sql is querying dbo.Protein . 您可能在不同的模式中有2个名为Protein表,因此NHibernate正在查询foo.Protein但是您的sql正在查询dbo.Protein

After that it starts to get more esoteric such as using custom dialects, entity tuplizers, etc. 之后,它开始变得更加神秘,例如使用自定义方言,实体修饰符等。

One simple way to see what is going on is to look at the SQL being issued, you can do this by: 查看正在发生的情况的一种简单方法是查看发出的SQL,您可以通过以下方法执行此操作:

  • Setting <property name="show_sql" value="true"/> and watching the console/logger. 设置<property name="show_sql" value="true"/>并查看控制台/记录器。
  • Using a profiling tool for your server (eg SQL Server Profiler for MSSQL). 为服务器使用性能分析工具(例如,用于MSSQL的SQL Server Profiler)。
  • Using NHibernate Profiler 使用NHibernate Profiler

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

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