简体   繁体   中英

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:

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.
  • A <discriminator> with force="true" attribute.
  • A <join> mapping that doesn't match on one side and isn't specified as optional="true" .
  • A custom <loader query-ref="..."> for the entity.
  • A <filter> on the entity that has been enabled through session.EnableFilter .
  • The class Protein isn't mapped to the Protein table.
  • You could have 2 tables named Protein in different schemas, so NHibernate is querying foo.Protein but your sql is querying 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:

  • Setting <property name="show_sql" value="true"/> and watching the console/logger.
  • Using a profiling tool for your server (eg SQL Server Profiler for MSSQL).
  • Using NHibernate Profiler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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