title: nHibernate 'Named Query Not Found' error from unit test builds
body: <p>I wonder if anyone can help me with an infuriating problem.</p>
  
  <p>We have a stored procedure in a SQL Server 2008 database that we call from nHibernate. We have various unit tests that test our data access repository, and when these run from our local Visual Studio machines they pass. However, when executed as part of our automated build on TFS 2010 against the same database, the repository code fails with:</p>
  
  <p>Named query not known: GetMyData</p>
  
  <p>The mapping XML looks like (I have simplified and removed company specific stuff):</p>
  
  <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="MyCompany.SystemDomain" namespace="MyCompany.SystemDomain"&gt;
  &lt;class name="MyClass"&gt;
      &lt;id name="Guid" type="guid"/&gt;
      &lt;property name="QuoteReference"/&gt;
  &lt;/class&gt;
  &lt;sql-query name="GetMyData"&gt;
      &lt;return alias="GetMyData" class="MyClass"&gt;
          &lt;return-property column="Guid" name="Guid"/&gt;
          &lt;return-property column="QuoteReference" name="QuoteReference"/&gt;
      &lt;/return&gt;       
      exec MyStoredProc :param1, :param2
  &lt;/sql-query&gt;
  </code></pre>
  
  <p></p>
  
  <p>The calling C# code looks like:</p>
  
  <pre><code>IQuery query = session.GetNamedQuery("GetMyData");
  query.SetString("QuoteReference", quoteReference);
  query.SetInt32("FSANumber", 12345);
  
  IList&lt;MyClass&gt; result = query.List&lt;MyClass&gt;();
  </code></pre>
  
  <p>I have checked that the .hbm.xml file is an embedded resource, and I have opened the repository assembly containing the above code with Reflector (taken from our build server in the Binaries folder) and can see the embedded resource containing the mapping, and I can see that the named query called using session.GetNamedQuery() call matches the name attribute in the sql-query mapping element in that assembly.</p>
  
  <p>I have also run SQL Profiler on our database when the automated build unit tests execute, and confirmed that the stored procedure is not being called during.</p>
  
  <p>Any help would be much appreciated, as we have completely run out of ideas.</p>
  
  <p>Many Thanks!</p>