我想在 SQL 中做一些工作,填充一个临时表,然后使用 NHibernate CreateSQLQuery 加入该临时表以获得我的最终结果。我们在 NHibernate 的 1.2.0.4000 版本上,我似乎在以后的查询中访问临时表时遇到问题,即使我在同一个会话中(我相信这意味着我在同一个 SQL 会话/连接中好)。下面是我的代码的简化版本
public void Work()
{
    SqlConnection connection = (SqlConnection)Session.Connection;
    SqlCommand command = new SqlCommand
                     {
                         CommandType = CommandType.Text,
                         CommandText = "SELECT ID = 1 INTO #TempTable",
                         Connection = connection,
                     };
    if ( Session.Transaction != null && Session.Transaction.IsActive )
    {
        Session.Transaction.Enlist( command );
    }
    command.ExecuteNonQuery();
    // Simplified example, I should have a temp table #TempTable with 1 row containing the values ID = 1
    // trying to fetch a list of Account objects where ID exists in #TempTable.
    // At this point, I get an error "Invalid object name '#TempTable'."
    IList<Account> accounts = Session.CreateSQLQuery(@"
        SELECT *
          FROM Account a
          JOIN #TempTable tt
            ON a.ID = tt.ID")
        .AddEntity("a", typeof(Account))
        .List<Account>();
    // Do some work on accounts list
}